2017-03-09 50 views
0

如何添加一些HTML文本表单字段?这是我的forms.py:我的HTML文件的Django - 添加文本模型表单字段

class SiteAddFormFull(forms.ModelForm): 

    url = forms.URLField(widget=forms.TextInput(attrs={'readonly': 'readonly'})) 

    class Meta: 
     model = Site 
     fields = ('url', 'name', 'description', 'keywords', 'group', 'category', 
       'subcategory', 'category1', 'subcategory1', 'email') 

    def clean(self): 
     cleaned_data = super().clean() 
     subcategory = cleaned_data['subcategory'] 
     subcategory1 = cleaned_data['subcategory1'] 
     if subcategory1 and (subcategory == subcategory1): 
      raise forms.ValidationError("Subcategories can't be the same.") 

部分:

<form method="post" action="" class="form-horizontal"> 
    {% csrf_token %} 
    {% bootstrap_form form_extended layout='horizontal'%} 
    {% bootstrap_button "Zatwierdź" size='large' button_type="submit" button_class="btn-primary" %} 
</form> 

我想以后 '组' 字段添加描述。喜欢的东西:

  • 您可以到2个类别
  • 您可以将网站加入网站2个小类

只是正常的HTML文本。我怎样才能做到这一点?

回答

2

你可以一个help_text属性添加到您的模型组领域:

class Site(models.Model): 
    group = models.<FieldType>(..., help_text="You can add site to 2 categories") 
+0

是否可以格式化(添加html标签)到help_text? – jundymek

+0

如果你把它们包装在'mark_safe()'中,这可能是可能的。虽然我没有尝试过。 – Tim

1

如果你想添加自定义HTML标记然后做到这一点:

class Meta: 
    model = Site 
    fields = ('url', 'name', 'description', 'keywords', 'group', 'category', 'subcategory', 'category1', 'subcategory1', 'email') 
    help_texts = { 
     'field_name': '<span class="my-class">Some useful help text.</span>', 
    } 

更多关于此here

0

好。我这样做:

group = models.CharField(max_length=10, choices=(('podstawowy', 'podstawowy'), 
               ('premium', 'premium')), default='podstawowy', 
         help_text="<div id='group'><ul><li>You can add site to 2 <b>categories</b></li></ul></div>") 

这里还有一个问题。如果会有3,4,5行的html代码,这里会有一些乱七八糟的代码。我应该如何写它?是否可以包含来自外部文件的一些HTML文本。也许另一个解决方案

+0

从我的观点来看,如果它只是3或4或5行代码而不会在其他地方重复使用,那么就没有必要将它们写入单独的模块中。像你一样在'help_text'中写入它们。 –