Django URLField和HTML5?

时间:2010-08-12 16:23:08

标签: django html5 django-forms

是否可以将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

1 个答案:

答案 0 :(得分:3)

您必须为此创建自定义小部件。

class URLInput(forms.TextInput):

    input_type = 'url'

然后您可以将此小部件传递给URLField构造函数:

class MyForm(forms.Form):

    url = forms.URLField(widget=URLInput())