django init函数与attrs在Form中 [英] django init function with attrs in Form
问题描述
以正常的方式,我的表单是完美的,但现在我想要一个垃圾更改,添加jquery重新填充一个选择窗体。
问题是Django配置文件从模型中创建表单,所以我想添加一个属性id选择表单(我还没有),但是然后选择不显示选项(值),我不明白为什么,选择表单将带有FK的另一个模型的值。
谢谢你们:)
对不起我的英文
我的自定义部分是:
def __init __(self,* args,** kwargs):
super(_ProfileForm,self).__ init __(* args,** kwargs)
self。字段['birth_date']。widget = widget = SelectDateWidget(years = range(datetime.date.today()。year,1900,-1))
self.fields ['sector']。widget = forms。选择(ATTRS = { '平变化':'get_vehicle _color();'})
django-profiles / profiles / utils.py
#....
def get_profile_form():
返回一个表单类
适用于创建/编辑特定于站点的用户
配置文件模型的实例,由AUTH_PROFILE_MODULE
设置定义。如果该设置丢失,请调用
``django.contrib.auth.models.SiteProfileNotAvailable``。
profile_mod = get_profile_model()
class _ProfileForm(forms.ModelForm):
class Meta:
model = profile_mod
exclude =('user',)#用户将被视图填充
def __init __(self,* args,** kwargs):
super(_ProfileForm ,self).__ init __(* args,** kwargs)
self.fields ['birth_date']。widget = widget = SelectDateWidget(years = range(datetime.date.today()。year,1900,-1 )
self.fields ['sector']。widget = forms.Select(attrs = {'onchange':'get_sector_code();'})
return _ProfileForm
你真正应该做的是处理你的jquery trhough Javascript,而不是通过python! / p>
Javascript
所以导入一个这样的js文件:
$(function(){
$('#id_sector')。onchange(f unction(){
get_vehicle_color();
})
})
< h3> Django表单html属性
对于那些真正需要为表单域设置属性的用户,可以这样做:
def __init __(self,* args,** kwargs):
super(_ProfileForm,self).__ init __(* args,** kwargs)
self.fields ['sector']。widget.attrs = {'my_attribute_key':'my_attribute_value'}
这种方法更清洁,因为您不要覆盖窗口小部件,并在修改模型或窗体类属性时将其关闭。
im using Django Profiles, and i need to make some change in the profile form.
In the normal way my form is fine completly, but now i would like a litter change, add jquery for repopulate a select form.
The problem is that Django Profile make the form from the model, so i would like to add a attribute id to the select form (i did yet), but then the select dont show me the options (values), i dont understand why, the select form bring the value from another model with FK.
Thanks guys :), Sorry with my English
my custom part is :
def __init__(self, *args, **kwargs):
super(_ProfileForm, self).__init__(*args, **kwargs)
self.fields['birth_date'].widget = widget=SelectDateWidget(years=range(datetime.date.today().year,1900,-1))
self.fields['sector'].widget= forms.Select(attrs={'onchange':'get_vehicle_color();'})
django-profiles/profiles/utils.py
#....
def get_profile_form():
"""
Return a form class (a subclass of the default ``ModelForm``)
suitable for creating/editing instances of the site-specific user
profile model, as defined by the ``AUTH_PROFILE_MODULE``
setting. If that setting is missing, raise
``django.contrib.auth.models.SiteProfileNotAvailable``.
"""
profile_mod = get_profile_model()
class _ProfileForm(forms.ModelForm):
class Meta:
model = profile_mod
exclude = ('user',) # User will be filled in by the view.
def __init__(self, *args, **kwargs):
super(_ProfileForm, self).__init__(*args, **kwargs)
self.fields['birth_date'].widget = widget=SelectDateWidget(years=range(datetime.date.today().year,1900,-1))
self.fields['sector'].widget= forms.Select(attrs={'onchange':'get_sector_code();'})
return _ProfileForm
What you really should do is to deal with your jquery trhough Javascript, not through python !
Javascript
So import a js file with something like this :
$(function(){
$('#id_sector').onchange(function(){
get_vehicle_color();
})
})
Django form html attribute
For those who actually need to set an attribute to a form field, you can do it as so :
def __init__(self, *args, **kwargs):
super(_ProfileForm, self).__init__(*args, **kwargs)
self.fields['sector'].widget.attrs = {'my_attribute_key':'my_attribute_value'}
This way is cleaner as you don't override the widget and get your head off when modifying your model or Form class attributes.
这篇关于django init函数与attrs在Form中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!