Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-ModelForm正在保存,即使数据无效_Python_Django_Django Templates_Django Forms - Fatal编程技术网

Python Django-ModelForm正在保存,即使数据无效

Python Django-ModelForm正在保存,即使数据无效,python,django,django-templates,django-forms,Python,Django,Django Templates,Django Forms,有点 我有一个这样定义的模型: class EditUserForm(UserForm): def __init__(self,data=None,instance=None): UserForm.__init__(self,data=data,instance=instance) del self.fields['username'] 我这样做是因为我想在我的EditUserForm上不显示username字段,但因为我在UserForm中重写了use

有点

我有一个这样定义的模型:

class EditUserForm(UserForm):
    def __init__(self,data=None,instance=None):
        UserForm.__init__(self,data=data,instance=instance)
        del self.fields['username']
我这样做是因为我想在我的EditUserForm上不显示username字段,但因为我在UserForm中重写了username字段,django中的一个bug阻止了它的工作,所以我以这种方式编写表单

我的视图代码如下所示:

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'
为了测试它,我尝试更改名字并使密码不匹配,当我提交时,我注意到打印的是无效的(密码验证错误正确出现),但在页面顶部的“欢迎(用户名)(用户名)”我注意到名字已更改。当我转到另一个页面时,它会返回到正确的值。我怎样才能避免这种情况发生

这是我的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 %}
{%if user.u经过身份验证%}
欢迎你
{%else%}
{%endif%}
我猜测,当我发布数据时,它会将user.first\u名称放在上下文中,覆盖实际的user.first\u名称。但它无效,因为数据尚未保存


那么,如何在发布到的模板中包含user.first\u名称,而不更改其值,直到其实际有效???

您的假设是正确的。验证后,
ModelForm
使用给定的
数据更新给定实例(
user
),使其处于无效状态

你可以:

  • 尝试创建
    用户
    的副本
  • 从数据库重新加载
    用户
  • 或者将
    user.first\u name
    存储在临时变量中,并在验证失败时将其设置回原位

编辑:请注意,尽管ModelForm修改了实例,但它不会保存它。

请显示该视图的其余代码。我想我可以做的是在我的UserProfile中创建一个“display\u name”字段,并使用该字段在模板中显示用户名。在UserProfile保存中,我将设置display\u name=user.first\u name+user.last\u name