Django username edit issue
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)
精彩评论