Django用户更新表单和视图 [英] Django User Update Form and View

查看:110
本文介绍了Django用户更新表单和视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django / Python开发过程中,我还处于早期阶段,在经过数小时/数天的头部刮擦和反复试验/错误后,我能够慢慢弄清大多数事情。我现在遇到一个常见问题,我无法正常工作:

I am very early on in my Django/Python development journey, most things I have been able to slowly figure out after a few hours/days of head scratching and trial/error. I now have the commonly asked question that I cannot get working correctly:

如何创建用户个人资料更新视图/表单?

我已经从Stack Overflow破解了几种解决方案,但是到目前为止我还不能弄清楚我做错了什么。这是我尝试使用Django 1.9的最初版本:

I have hacked on several solutions from Stack Overflow, and just cannot figure out what I am doing wrong thus far. Here is the initial version of my poor attempt using Django 1.9:

#forms.py
class profileForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email']

#views.py
@login_required
def profile(request):
    if request.method == 'POST':
        form = profileForm(data=request.POST, instance=request.user)
        update = form.save(commit=False)
        update.user = request.user
        update.save()
    else:
        form = profileForm(instance=request.user)

    return render(request, 'profile.html', {'form': form})

#urls.py
urlpatterns = [
    url(r'^dashboard/$', views.dashboard, name='dashboard'),
    url(r'^dashboard/profile$', views.profile, name='profile'),
]

#profile.html
<form action="." method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Update Profile</button>
</form>

因此,我的更新用户个人资料视图的当前行为是不更新POST上的表单,但该页面确实加载到其他URL。例如,配置文件更新表单的URL是 / dashboard / profile ,它在表单提交时重定向到 / dashboard 。如果我在Django管理员中手动添加email / first_name / last_name,它会正确显示在我的表单中...我只是无法获取它来保存更新的数据。

So the current behaviour of my update user profile view is to not update any of the specified fields in the form on POST, but the page does load to a different URL. For example the URL for the profile update form is /dashboard/profile which redirects to /dashboard on form submission. If I manually add email/first_name/last_name in the Django admin, it is displayed correctly in my form... I just cannot get it to save updated data.

我上面的代码的版本是否也给我用户名错误,我猜这可能已经接近解决方案了?最终,我想使用电子邮件地址作为用户名,但是一旦我拥有了一个非常简单的工作资料更新表格,我想自己弄清楚这一点。

I did have a version of the above code that was giving me username errors also, which I am guessing might have been close to a solution? Eventually I would like to use the email address as the username, but I would like to figure that out for myself once I have an ultra-simple working profile update form.

对于这个愚蠢的问题,我事先表示歉意,我在Google周围搜索并尝试了其他解决方案,但是我要么无法获得相同的初始错误,要么无法获得详细的工作版本。

I apologise in advance for the silly question, I have Googled around and attempted other solutions but I either cannot get the same initial errors or cannot get a working version as detailed.

谢谢大家,度过一个美好的白天/夜晚!

Thanks all and have a great day/night!

编辑:

I也尝试过此解决方案如何在不创建新对象的情况下更新User对象?,它也具有相同的行为:不会在POST上更新表单中的任何指定字段,但是页面确实会加载到其他URL ...这使我更加困惑,也许问题出在我的url上

I have also tried this solution How to update User object without creating new one?, which also has the same behaviour: not update any of the specified fields in the form on POST, but the page does load to a different URL... which confuses me more, maybe the issue is with my urls.py instead?

推荐答案

这里没有针对用户的特定内容。您有两个问题:第一个是由于您的URL。您的个人资料视图不会以斜杠结尾,因此表单的动作为。

There's nothing specific to users here. You have two problems: the first is because of your urls. Your profile view does not end in a slash, so the form's action of "." sends it to the parent, "/dashboard", and it does not go through the processing code at all.

url(r'/dashboard/profile/$'...)

第二,该模型不会更新,因为您尚未调用表单的验证方法。

Secondly, the model will not be updated because you have not called the form's valdation method.

if request.method == 'POST':
    form = profileForm(data=request.POST, instance=request.user)
    if form.is_valid():
        form.save()
        return redirect('somewhere')

注意,这里没有理由使用commit = False东西;该实例已经是用户。

Note there is no reason to use the commit=False stuff here; the instance is already the user.

这篇关于Django用户更新表单和视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆