def formfield(self, **kwargs): defaults = {'widget': UEditorWidget(attrs=self.ueditor_settings)} defaults.update(kwargs) if defaults['widget'] == admin_widgets.AdminTextareaWidget: defaults['widget'] = AdminUEditorWidget( attrs=self.ueditor_settings) return super(UEditorField, self).formfield(**defaults)
def formfield(self, **kwargs): defaults = { 'widget': UEditorWidget(**None) } defaults.update(kwargs) if defaults['widget'] == admin_widgets.AdminTextareaWidget: defaults['widget'] = AdminUEditorWidget(**None) return super(UEditorField, self).formfield(**None)
class PageForm(forms.ModelForm): content = forms.CharField( label="内容", widget=UEditorWidget(attrs={'toolbars': 'besttome', 'width': '700', 'height': '400', })) class Meta: model = Page
class CommentForm(forms.Form): """ 提交评论表单 """ content_type = forms.CharField(widget=forms.HiddenInput) object_id = forms.IntegerField(widget=forms.HiddenInput) text = forms.CharField(widget=UEditorWidget( attrs={"width": 'auto', "height": 'auto', "toolbars": [['fullscreen', 'source', 'undo', 'redo', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist','selectall', 'cleardoc', 'emotion']]}), error_messages={'required': '评论内容不能为空'}) # text = forms.CharField(widget=CKEditorWidget(config_name='comment_ckeditor'), # error_messages={'required': '您尚未写任何评论内容'}) reply_comment_id = forms.IntegerField(widget=forms.HiddenInput(attrs={'id': 'reply_comment_id'})) def __init__(self, *args, **kwargs): if 'user' in kwargs: self.user = kwargs.pop('user') super(CommentForm, self).__init__(*args, **kwargs) def clean(self): # 验证用户是否处在登录状态 if self.user.is_authenticated: self.cleaned_data['user'] = self.user else: raise forms.ValidationError('您尚未登录,请先登录才能评论') # 评论对象验证 content_type = self.cleaned_data['content_type'] object_id = self.cleaned_data['object_id'] # 找到post对象 try: models_class = ContentType.objects.get(model=content_type).model_class() models_obj = models_class.objects.get(pk=object_id) self.cleaned_data['content_object'] = models_obj except ObjectDoesNotExist: raise forms.ValidationError('评论对象不存在') return self.cleaned_data def clean_reply_comment_id(self): reply_comment_id = self.cleaned_data['reply_comment_id'] if reply_comment_id < 0: raise forms.ValidationError('回复出错') elif reply_comment_id == 0: self.cleaned_data['parent'] = None elif Comment.objects.filter(pk=reply_comment_id).exists(): self.cleaned_data['parent'] = Comment.objects.get(pk=reply_comment_id) else: raise forms.ValidationError('回复出错') return reply_comment_id
def get_field_style(self, db_field, style, **kwargs): if style in ('ueditor', ): ue = UEditorWidget(width=800, height=500, imageManagerPath="post/", imagePath='post/', filePath='post/', toolbars='normal') attrs = {'widget': ue} return attrs
def __init__(self, label, width=600, height=300, toolbars="full", imagePath="", filePath="", upload_settings={}, settings={}, command=None, event_handler=None, *args, **kwargs): uSettings = locals().copy() del uSettings["self"], uSettings[ "label"], uSettings["args"], uSettings["kwargs"] kwargs["widget"] = UEditorWidget(attrs=uSettings) kwargs["label"] = label super(UEditorField, self).__init__(*args, **kwargs)
class TestUEditorForm(forms.Form): Name = forms.CharField(label=u'姓名') ImagePath = forms.CharField() Description = UEditorField(u"描述", initial="abc", width=1000, height=300) Content = forms.CharField(label=u"内容", widget=UEditorWidget({ "width": 600, "height": 100, "imagePath": 'aa', "filePath": 'bb', "toolbars": "full" }))
class PostForm(forms.ModelForm): content = forms.CharField( label="内容", widget=UEditorWidget(attrs={'toolbars': 'besttome', 'width': '700', 'height': '400', })) summary = forms.CharField(label=u'摘要', required=False, widget=forms.Textarea(attrs={'style': 'width:500px;height:100px;'})) class Meta: model = Post fields = ('title', 'alias', 'is_top', 'is_old', 'pub_time', 'tags', 'status', 'category', 'summary', 'content')
class DetailContentForm(forms.Form): bbs_answer_Content = forms.CharField(label=u"内容", widget=UEditorWidget({ "width": 600, "height": 100, "imagePath": 'aa', "filePath": 'bb', "toolbars": "full" }))
class InsertContentForm(forms.Form): bbs_contitle = forms.CharField(max_length=30) bbs_consmarty = forms.CharField(max_length=100) image = forms.ImageField() bbs_content = forms.CharField(label=u"内容", widget=UEditorWidget({ "width": 600, "height": 100, "imagePath": 'aa', "filePath": 'bb', "toolbars": "full" })) bbs_category = forms.CharField(max_length=20) sex_old = forms.CharField(max_length=5)
class EventAdmin(object): def show_page(self, event): return '<a href="%s"/><i class="fa fa-eye"></i></a>' % self.get_admin_url('event_show', event.id) show_page.short_description = "查看" show_page.allow_tags = True list_display = ('title', 'event_type', 'public_time', 'start_time', 'end_time', 'author', 'is_active', 'show_page') list_filter = ('public_time', 'event_type', 'public_time', 'start_time', 'end_time', 'author', 'is_active') search_fields = ('slug',) model_icon = 'fa fa-bullhorn' user_fields = ('author',) inlines = (EventMemberInline, FormFieldInlineAdmin, ) formfield_overrides = {UEditorField: {'widget': UEditorWidget({'width':'100%', 'height':300, 'toolbars':"full", 'imagePath':"event/img/", 'filePath':"event/file/"})}}
class productform(forms.Form): catelist = productcate.objects.filter(cate__isnull=False) name = forms.CharField(max_length=100, label="产品名称", widget=widgets.TextInput(attrs={'size': '50%'})) cate = forms.ModelChoiceField(queryset=catelist, label="类别", initial=catelist.first().name) img = forms.ImageField(label="产品图片", allow_empty_file=True, required=False) price = forms.IntegerField(label="产品价格", initial=100) repository = forms.ChoiceField(label="库存", choices=(("无货", "无货"), ("有货", "有货")), initial="有货") content = forms.CharField(label="产品详情", widget=UEditorWidget({ "width": "98%", "height": 400, "imagePath": 'pic/', "filePath": 'upfiles/' }))
class Meta: model = Chapter fields = ['pid', 'book_id', 'title', 'code', 'sort', 'content'] widgets = { 'book_id': forms.HiddenInput(), 'title': forms.TextInput(attrs={'class': 'form-control'}), 'code': forms.TextInput(attrs={'class': 'form-control'}), 'sort': forms.TextInput(attrs={'class': 'form-control'}), 'content': UEditorWidget( attrs={'width': '100%', 'height': 400, 'imagePath': 'upload/images/', 'filePath': 'upload/files/', 'toolbars': [[ 'fullscreen', 'source', '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', '|', 'simpleupload', 'insertimage', 'emotion', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertcode', 'webapp', 'pagebreak', 'template', '|', 'selectall', 'cleardoc' ]]}, ), }
class newsform(forms.Form): catelist = cate.objects.filter(pcate__isnull=False) #二级和三级分类 fcatelist = cate.objects.filter(pcate__isnull=True) #一级分类 scatelist = cate.objects.filter(pcate__in=fcatelist) #二级分类 tcatelist = cate.objects.filter(pcate__in=scatelist) #三级分类 stcatelist = scatelist | tcatelist #二级和三级分类合并 cate = forms.ModelChoiceField(queryset=stcatelist, label="类别", initial=catelist.first().name) title = forms.CharField(max_length=100, label="标题", widget=widgets.TextInput(attrs={'size': '50%'})) img = forms.ImageField(label="图片封面", allow_empty_file=True, required=False) content = forms.CharField(label="内容", widget=UEditorWidget({ "width": "98%", "height": 400, "imagePath": 'pic/', "filePath": 'upfiles/' }))
def createField(self, field=None, extra_val=None, is_update=False): blank = field['blank'] default = extra_val if is_update else field['default'] if field['type'] == FieldUtil.FIELD_TEXT: self.fields[field['name']] = forms.CharField( label=field['title'], max_length=int(field['length']), required=blank, initial=default, help_text=field['help_text'], widget=forms.TextInput(attrs={'class': 'form-control'}, )) elif field['type'] == FieldUtil.FIELD_TEXTAREA: self.fields[field['name']] = forms.CharField( label=field['title'], max_length=int(field['length']), required=blank, initial=default, help_text=field['help_text'], widget=forms.Textarea(attrs={'class': 'form-control'}, )) elif field['type'] == FieldUtil.FIELD_EDITOR: self.fields[field['name']] = forms.CharField( label=field['title'], max_length=int(field['length']), required=blank, initial=default, help_text=field['help_text'], widget=UEditorWidget( attrs={ 'width': '100%', 'height': 400, 'imagePath': 'upload/images/', 'filePath': 'upload/files/', 'toolbars': [[ 'fullscreen', 'source', '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', '|', 'simpleupload', 'insertimage', 'emotion', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertcode', 'webapp', 'pagebreak', 'template', '|', 'selectall', 'cleardoc' ]] })) elif field['type'] == FieldUtil.FIELD_NUMBER: self.fields[field['name']] = forms.CharField( label=field['title'], max_length=int(field['length']), required=blank, initial=int(default), help_text=field['help_text'], widget=forms.NumberInput(attrs={'class': 'form-control'}, )) elif field['type'] == FieldUtil.FIELD_SELECT: self.fields[field['name']] = forms.ChoiceField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], widget=forms.Select(attrs={'class': 'form-control'}, )) self.fields[field['name']].choices = FieldUtil.getOptions( field['value']) elif field['type'] == FieldUtil.FIELD_CHECKBOX: self.fields[field['name']] = forms.BooleanField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], widget=forms.CheckboxInput()) elif field['type'] == FieldUtil.FIELD_RADIO: self.fields[field['name']] = forms.BooleanField( label=field['title'], required=blank, initial=int(default), help_text=field['help_text'], widget=forms.RadioSelect(attrs={}, )) elif field['type'] == FieldUtil.FIELD_IMAGE: default = extra_val if is_update else None self.fields[field['name']] = forms.ImageField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], ) elif field['type'] == FieldUtil.FIELD_FILE: default = extra_val if is_update else None self.fields[field['name']] = forms.FileField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], ) elif field['type'] == FieldUtil.FIELD_DATETIME: default = extra_val if is_update else None self.fields[field['name']] = forms.DateTimeField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], ) elif field['type'] == FieldUtil.FIELD_DATE: default = extra_val if is_update else None self.fields[field['name']] = forms.DateField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], ) elif field['type'] == FieldUtil.FIELD_TIME: default = extra_val if is_update else None self.fields[field['name']] = forms.TimeField( label=field['title'], required=blank, initial=default, help_text=field['help_text'], ) elif field['type'] == FieldUtil.FIELD_EMAIL: self.fields[field['name']] = forms.EmailField( label=field['title'], required=blank, help_text=field['help_text'], initial=default, widget=forms.ClearableFileInput( attrs={'class': 'form-control'}, )) elif field['type'] == FieldUtil.FIELD_DECIMAL: default = field['default'] if decimal.Decimal( field['default']) else None default = extra_val if is_update else default self.fields[field['name']] = forms.DecimalField( label=field['title'], required=blank, help_text=field['help_text'], initial=default, widget=forms.NumberInput(attrs={'class': 'form-control'}, )) elif field['type'] == FieldUtil.FIELD_FLOAT: default = field['default'] if float(field['default']) else None default = extra_val if is_update else default self.fields[field['name']] = forms.FloatField( label=field['title'], required=blank, help_text=default, initial=float(field['default']), widget=forms.NumberInput(attrs={'class': 'form-control'}, ))
class TestUEditorForm(forms.Form): Name=forms.CharField('姓名') ImagePath=forms.CharField() Description=UEditorField("描述",initial="abc",width=600,height=800) Content=forms.CharField(label="内容",widget=UEditorWidget(width=800,height=500, imagePath='aa', filePath='bb',toolbars={}))
def __init__(self,label,width=600,height=300,plugins=(),toolbars="mini",filePath="",imagePath="",scrawlPath="",imageManagerPath="",css="",options={}, *args, **kwargs): uOptions = MadeUeditorOptions(width,height,plugins,toolbars,filePath,imagePath,scrawlPath,imageManagerPath,css,options) kwargs["widget"] = UEditorWidget(**uOptions) kwargs["label"] = label super(UEditorField,self).__init__( *args, **kwargs)
class BlogUEditorForm(forms.Form): blog_title = forms.CharField(label=u'title') blog_context = forms.CharField(label=u"context", widget=UEditorWidget({"width":600, "height":500, "imagePath":'blog_images', "filePath":'blog_file', "toolbars":"full"}))