嗨,我正在尝试使用一个名为Django的django插件- countries与所有国家一起添加一个select,到目前为止它还在工作,但是我如何在select中添加一个html类呢?
发布于 2019-02-14 01:28:42
在Django中,表单字段使用小部件呈现为HTML表示。
通常,在定义表单时,可以将小部件呈现类和其他HTML属性指定为小部件的参数。例如:
from django import forms
class CommentForm(forms.Form):
comment = forms.CharField(
widget=forms.Textarea(attrs={"class": "my-class"})
)考虑到您的问题与django-国家中的自定义字段有关,在实例化CountrySelectWidget实例时,您应该通过传递自定义属性来尝试相同的方法。例如:
from django import forms
from django_countries.fields import CountryField
from django_countries.widgets import CountrySelectWidget
class CustomForm(forms.Form):
country = CountryField().formfield(
widget=CountrySelectWidget(
attrs={"class": "my-class"}
)
)https://stackoverflow.com/questions/54680801
复制相似问题