0

用户使用Django Userena编辑其个人资料的能力突然不起作用。它只是返回到同一个编辑页面而不做任何事情。任何人都可以看到代码有什么问题。谢谢

表格.py

class EditProfileForm(forms.ModelForm):
""" Base form used for fields that are always required """
first_name = forms.CharField(label=_(u'First name'),
                             max_length=30,
                             required=False)
last_name = forms.CharField(label=_(u'Last name'),
                            max_length=30,
                            required=False)

def __init__(self, *args, **kw):
    super(EditProfileForm, self).__init__(*args, **kw)
    # Put the first and last name at the top
    new_order = self.fields.keyOrder[:-2]
    new_order.insert(0, 'first_name')
    new_order.insert(1, 'last_name')
    self.fields.keyOrder = new_order

class Meta:
    model = get_profile_model()
    exclude = ['user']

def save(self, force_insert=False, force_update=False, commit=True):
    profile = super(EditProfileForm, self).save(commit=commit)
    # Save first and last name
    user = profile.user
    user.first_name = self.cleaned_data['first_name']
    user.last_name = self.cleaned_data['last_name']
    user.save()

    return profile

视图.py

enter code heredef profile_edit(request, username, edit_profile_form=EditProfileForm,
             template_name='userena/profile_form.html', success_url=None,
             extra_context=None, **kwargs):
"""
Edit profile.

Edits a profile selected by the supplied username. First checks
permissions if the user is allowed to edit this profile, if denied will
show a 404. When the profile is successfully edited will redirect to
``success_url``.

:param username:
    Username of the user which profile should be edited.

:param edit_profile_form:

    Form that is used to edit the profile. The :func:`EditProfileForm.save`
    method of this form will be called when the form
    :func:`EditProfileForm.is_valid`.  Defaults to :class:`EditProfileForm`
    from userena.

:param template_name:
    String of the template that is used to render this view. Defaults to
    ``userena/edit_profile_form.html``.

:param success_url:
    Named URL which will be passed on to a django ``reverse`` function after
    the form is successfully saved. Defaults to the ``userena_detail`` url.

:param extra_context:
    Dictionary containing variables that are passed on to the
    ``template_name`` template.  ``form`` key will always be the form used
    to edit the profile, and the ``profile`` key is always the edited
    profile.

**Context**

``form``
    Form that is used to alter the profile.

``profile``
    Instance of the ``Profile`` that is edited.

"""
user = get_object_or_404(get_user_model(),
                         username__iexact=username)

profile = user.get_profile()

user_initial = {'first_name': user.first_name,
                'last_name': user.last_name}

form = edit_profile_form(instance=profile, initial=user_initial)

if request.method == 'POST':
    form = edit_profile_form(request.POST, request.FILES, instance=profile,
                             initial=user_initial)

    if form.is_valid():
        profile = form.save()

        if userena_settings.USERENA_USE_MESSAGES:
            messages.success(request, _('Your profile has been updated.'),
                             fail_silently=True)

        if success_url: redirect_to = success_url
        else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username})
        return redirect(redirect_to)

if not extra_context: extra_context = dict()
extra_context['form'] = form
extra_context['profile'] = profile
return ExtraContextTemplateView.as_view(template_name=template_name,
                                        extra_context=extra_context)(request)
4

1 回答 1

1

您必须运行以下命令才能解决问题

python manage.py check_permissions

我希望它能解决你的问题。干杯。

于 2014-07-01T04:25:23.363 回答