开发者

Add property to one of the form fields in django

I am trying to add a speci开发者_高级运维al template property to one of the form fields which I will than use to render the form in template tag. Here is the code:

form = ProfileForm()

for field in form:
    if field.name == 'email':
       field.template = 'email_field.html'

This way, original form variable is not modified. Is there a way to achive my goal?


I'm going to assume you might want to build a html5 email field:

from django import forms
from django.forms.widgets import Widget
from django.utils.safestring import mark_safe


class Html5Email(Widget):

    def render(self, name, value, attrs=None):
        return mark_safe(u'<input name="custom-email" type="email" />')


class YourForm(forms.Form):
    html5_email = forms.CharField(widget=Html5Email())

I came up with the above by glancing at the Django source code. Since I haven't personally use the above in an actual project, the code will probably need to be fleshed out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜