开发者

Django username edit issue

开发者 https://www.devze.com 2023-03-20 18:49 出处:网络
I want to allow users to change their usernames, so I have a form: class UserForm(models.ModelForm): class Meta:

I want to allow users to change their usernames, so I have a form:

class UserForm(models.ModelForm):

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name')

In template, I have a greeting block, something like Hello, {{ request.user.username }}

When I submit a form with a username that already exists, it throws an error "User with this Userame already exists.", but in the greeting block I see Hello, username (where username is this one I have submitted).

What am I doing wrong? Anyone faced something similar?

Here is a view code:

@login_required
def persona_edit(request):

    form = PersonaForm(request.POST开发者_如何学编程 or None, instance=request.user.persona)
    user = User.objects.get(pk=request.user.pk)
    user_form = UserForm(request.POST or None, instance=user)

    if form.is_valid() and user_form.is_valid():
        form.save()
        user_form.save()
        messages.success(request, _(u'Profile updated'))
        return redirect('identity:dashboard')

    payload = {'form': form, 'user_form': user_form}
    return render(request, 'identity/persona_edit.html', payload)
0

精彩评论

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