是否可以将django的(v1.2)URLField输出为input
的HTML5 type="url"
标记?
------------- SOLUTION -------------
from django.forms import ModelForm
from django.forms import widgets
from django.forms import fields
from models import MyObj
class URLInput(widgets.Input):
input_type = 'url'
class MyObjForm(ModelForm):
url = fields.URLField(widget=URLInput())
class Meta:
model = MyObj
答案 0 :(得分:3)
您必须为此创建自定义小部件。
class URLInput(forms.TextInput):
input_type = 'url'
然后您可以将此小部件传递给URLField构造函数:
class MyForm(forms.Form):
url = forms.URLField(widget=URLInput())