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

查看:29
本文介绍了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.

我提前为这个愚蠢的问题道歉,我在谷歌上搜索并尝试了其他解决方案,但我要么无法获得相同的初始错误,要么无法获得详细的工作版本.

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!

我也试过这个解决方案 如何更新用户对象没有创建新的?,它也有相同的行为:在 POST 时不更新表单中的任何指定字段,但页面确实加载到不同的 URL ......这让我更加困惑,也许是问题是用我的 urls.py 代替吗?

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?

推荐答案

这里没有针对用户的任何内容.您有两个问题:第一个是因为您的网址.您的个人资料视图不以斜线结尾,因此表单的."操作.将其发送给父级/dashboard",它根本不经过处理代码.

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天全站免登陆