'用户'对象没有属性' get'在具有初始数据的ModelForm中

时间:2015-11-17 21:43:16

标签: python django django-forms

我正在创建具有初始数据的ModelForms。 UserSettingsForm工作,但AddressSettingsForm不工作。它给出了AttributeError' User'对象没有属性' get'。我创建AddressSettingsForm的方式与我创建UserSettingsForm的方式相同,但是它给出了一个错误。 Django调试页面没有给出错误来源。它只是告诉我错误是在AddressSettingsForm中。有谁看到我的错误?

forms.py

class UserSettingsForm(forms.ModelForm):
    class Meta:
        model = User
        exclude = ['role', 'address', 'is_active', 'is_admin', 'last_login', 'password']

    def __init__(self, user, *args, **kwargs):
        super(UserSettingsForm, self).__init__(*args, **kwargs)
        self.fields['first_name'].initial = user.first_name
        self.fields['last_name'].initial = user.last_name
        self.fields['email'].initial = user.email
        self.fields['phone'].initial = user.phone

class AddressSettingsForm(forms.ModelForm):
    class Meta:
        model = Address
        exclude = ['name']

    def __init__(self, address, *args, **kwargs):
        super(AddressSettingsForm, self).__init__(*args, **kwargs)
        self.fields['street'].initial = address.street
        self.fields['city'].initial = address.city
        self.fields['state'].initial = address.state
        self.fields['zip'].initial = address.zip

views.py

@login_required
def settings(request, template):
    if request.method == 'POST':
        user_settings_form = UserSettingsForm(request.POST, request.user)
        address_settings_form = AddressSettingsForm(request.POST, request.user.address)

        if user_settings_form.is_valid() and address_form.is_valid():
            address = Address(name=request.user.first_name + ' ' + request.user.last_name, **address_form.cleaned_data)
            user = request.user

            user.first_name = user_settings_form.cleaned_data['first_name']
            user.last_name = user_settings_form.cleaned_data['last_name']
            user.email = user_settings_form.cleaned_data['email']
            user.phone = user_settings_form.cleaned_data['phone']
            user.address = address
            user.save()

    else:
        user_settings_form = UserSettingsForm(request.user)
        address_settings_form = AddressForm(request.user)

    return render(request, template, { 'user_settings_form': user_settings_form, 'address_settings_form': address_settings_form, 'path' : request.path, 'is_logged_in': request.user.is_authenticated })

错误:

AttributeError at /settings/
'User' object has no attribute 'get'

模板:

21                  </div>
22  
23                  <div class='col-sm-2'></div>
24              </div><br>
25  
26              <div class='row'>
27                  <div class='col-sm-2'></div>
28  
29                  <div class='col-sm-4'>
30                      {% bootstrap_form user_settings_form %}
31  

      {% bootstrap_form address_settings_form %}



32                  </div>
33  
34                  <div class='col-sm-4'>
35
36                  </div>
37  
38                  <div class='col-sm-2'></div>
39              </div><br>
40  
41              <div class='row y-center'>

回溯:

/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
                                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view
                            return view_func(request, *args, **kwargs) ...
▶ Local vars
/Users/kevin/yaardvark/web/views.py in settings
                return render(request, template, { 'user_settings_form': user_settings_form, 'address_settings_form': address_settings_form, 'profile_form': profile_form, 'path' : request.path, 'is_logged_in': request.user.is_authenticated }) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/shortcuts.py in render
                        template_name, context, request=request, using=using) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/loader.py in render_to_string
                    return template.render(context, request) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/backends/django.py in render
                    return self.template.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in render
                                return self._render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in _render
                    return self.nodelist.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in render
                            bit = self.render_node(node, context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/debug.py in render_node
                        return node.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/loader_tags.py in render
                    return compiled_parent._render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in _render
                    return self.nodelist.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in render
                            bit = self.render_node(node, context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/debug.py in render_node
                        return node.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/loader_tags.py in render
                            result = block.nodelist.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in render
                            bit = self.render_node(node, context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/debug.py in render_node
                        return node.render(context) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/template/base.py in render
                                return func(*resolved_args, **resolved_kwargs) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/bootstrap3/templatetags/bootstrap3.py in bootstrap_form
                return render_form(*args, **kwargs) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/bootstrap3/forms.py in render_form
                return renderer_cls(form, **kwargs).render() ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/bootstrap3/renderers.py in render
                    return mark_safe(self._render()) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/bootstrap3/renderers.py in _render
                    return self.render_errors() + self.render_fields() ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/bootstrap3/renderers.py in render_errors
                        form_errors = self.get_fields_errors() + self.form.non_field_errors() ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/forms/forms.py in non_field_errors
                    return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield')) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/forms/forms.py in errors
                        self.full_clean() ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/forms/forms.py in full_clean
                    self._clean_fields() ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/forms/forms.py in _clean_fields
                        value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/forms/widgets.py in value_from_datadict
                    return data.get(name, None) ...
▶ Local vars
/Users/kevin/.virtualenvs/yaardvark/lib/python2.7/site-packages/django/utils/functional.py in inner
                    return func(self._wrapped, *args) ...
▶ Local vars

1 个答案:

答案 0 :(得分:2)

views.py最终else条款中,您正使用AddressForm初始化request.user。您在request.user.address传递的代码中的上方。您还试图初始化AddressForm而不是AddressSettingsForm

两种__init__方法都希望useraddress作为第一个参数。然而,如果它是POST请求,则在POST数据之后将其作为第二个参数传递。尝试交换订单。

这样做:

if request.method == 'POST':
    user_settings_form = UserSettingsForm(request.user, request.POST)
    address_settings_form = AddressSettingsForm(request.user.address, request.POST)

...

else:
    user_settings_form = UserSettingsForm(request.user)
    address_settings_form = AddressSettingsForm(request.user.address)