【发布时间】:2011-01-25 03:46:28
【问题描述】:
有点。
我有一个这样定义的模型:
class EditUserForm(UserForm):
def __init__(self,data=None,instance=None):
UserForm.__init__(self,data=data,instance=instance)
del self.fields['username']
我这样做是因为我想排除在我的 EditUserForm 上显示的用户名字段,但是因为我覆盖了 UserForm 中的用户名字段,django 中的一个错误阻止了它的工作,所以我以这种方式编写了表单。
我的视图代码如下所示:
if request.method == 'POST':
uf = EditUserForm(request.POST,instance=user)
upf = StudentProfileForm(request.POST,instance=profile)
if uf.is_valid() and upf.is_valid():
print 'VALID'
uf.save()
upf.save()
messages.success(request, 'You have successfully updated your profile.')
return HttpResponseRedirect('/student/editprofile')
else:
print 'INVALID'
为了测试它,我尝试更改名字并使密码不匹配,当我提交时我注意到打印了 INVALID(密码验证错误正确出现)但在页面顶部我有“欢迎( users first name) (the users last name)” 我注意到名字已经改变了。当我转到不同的页面时,它会回到正确的值。我怎样才能避免这种情况发生?
这是我的html代码:
{% if user.is_authenticated %}
Welcome, <a href = "/{{user.get_profile.getType}}/profile">{{user.first_name}} {{user.last_name}}</a> |
<a href = "/auth/logout">Logout</a>
{% else %}
{% endif %}
我对正在发生的事情的猜测是,当我发布数据时,它会将 user.first_name 置于覆盖实际的 user.first_name 的上下文中。但它是无效的,因为数据还没有保存...
那么,我怎样才能在发布到的模板中包含 user.first_name 而不会更改它的值,直到它真正有效??
【问题讨论】:
-
请显示该视图的其余代码。
标签: python django django-templates django-forms