Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django URLField and HTML5?

Is it possible to make django's (v1.2) URLField output an HTML5 input tag where 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
like image 937
Roger Avatar asked Aug 16 '26 14:08

Roger


1 Answers

You have to create a custom widget for that.

class URLInput(forms.TextInput):

    input_type = 'url'

Then you can pass this widget to URLField constructor:

class MyForm(forms.Form):

    url = forms.URLField(widget=URLInput())
like image 100
Andrey Fedoseev Avatar answered Aug 18 '26 07:08

Andrey Fedoseev