django init函数的形式是attrs

时间:2021-03-14 18:18:53

im using Django Profiles, and i need to make some change in the profile form.

我使用Django概要文件,我需要在概要文件表单中做一些更改。

In the normal way my form is fine completly, but now i would like a litter change, add jquery for repopulate a select form.

按照通常的方式,我的表单完全没问题,但是现在我想要做一点更改,添加jquery来重新填充选择表单。

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.

问题是Django概要形式从模型中,所以我想添加一个属性id选择形式(我),但选择不给我选项(值),我不明白为什么,选择形式带来的价值与颗另一个模型。

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

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

2 个解决方案

#1


2  

What you really should do is to deal with your jquery trhough Javascript, not through python !

您真正应该做的是处理jquery trhough Javascript,而不是通过python !

Javascript

So import a js file with something like this :

导入一个像这样的js文件:

$(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.

这种方式更简洁,因为在修改模型或表单类属性时,您不需要重写小部件,也不必惊慌失措。

#2


0  

Try this:

试试这个:

 class _ProfileForm(forms.ModelForm):
     birth_date = forms.DateTimeField(
         widget = SelectDateWidget(years=range(datetime.date.today().year,1900,-1))
     )
     sector = forms.ChoiceField(
         widget = Select(attrs={'onchange':'get_sector_code();'})
     )

     class Meta:
         model = profile_mod
         exclude = ('user',) # User will be filled in by the view.

#1


2  

What you really should do is to deal with your jquery trhough Javascript, not through python !

您真正应该做的是处理jquery trhough Javascript,而不是通过python !

Javascript

So import a js file with something like this :

导入一个像这样的js文件:

$(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.

这种方式更简洁,因为在修改模型或表单类属性时,您不需要重写小部件,也不必惊慌失措。

#2


0  

Try this:

试试这个:

 class _ProfileForm(forms.ModelForm):
     birth_date = forms.DateTimeField(
         widget = SelectDateWidget(years=range(datetime.date.today().year,1900,-1))
     )
     sector = forms.ChoiceField(
         widget = Select(attrs={'onchange':'get_sector_code();'})
     )

     class Meta:
         model = profile_mod
         exclude = ('user',) # User will be filled in by the view.