简体   繁体   English

当前用户的Django用户配置文件模型表单编辑

[英]Django User profile model form edit for current user

I have searched a lot on this and all the questions and answers provided are never clear and fit for purpose, maybe I'm going at it the wrong way.我已经对此进行了大量搜索,并且提供的所有问题和答案都从来没有明确且适合目的,也许我走错了路。 I have just recently started Python and Django, so know very little about it.我最近才开始使用 Python 和 Django,所以对它知之甚少。 I am creating a website and have done the basic authentication using Django and even added the facebook authentication using social-django.我正在创建一个网站,并使用 Django 完成了基本身份验证,甚至使用社交 django 添加了 facebook 身份验证。 That part all works fine.那部分一切正常。 Now I am moving to profile information and how to update it after you sign up to website.现在我将转向个人资料信息以及在您注册网站后如何更新它。

I have the below UserProfile in models.py:我在models.py中有以下UserProfile:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.TextField()
    last_name = models.TextField()
    email_address = models.EmailField()
    phone_number = models.TextField()

In forms.py a testing form:在 forms.py 一个测试表单:

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['first_name','last_name','email_address']

Then in views.py I have a function:然后在 views.py 我有一个功能:

def myprofile(request):
    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=request.user)
        if form.is_valid():
            form.save()
            user_profile = UserProfile.objects.get(user=request.user)
    if request.method == 'GET':
        user_profile = UserProfile.objects.get(user=request.user)
        form = UserProfileForm(instance=user_profile)

    return render(request, 'pandp/myprofile.html',
                 {'user_profile' : user_profile, 'form' : form})

And finally myprofile.html:最后是 myprofile.html:

<div class="profile-details">
   <form method="post">{% csrf_token %}
     {{ form.as_p }}
     <input type="submit" value="Update">
   </form>
</div>

I was struggling from the start to actually only load UserProfile of the logged in User, it seems like I managed to do it using UserProfile.objects.get(user=request.user) and UserProfileForm(request.POST, instance=request.user) but not really sure if it's the right way to do it.我从一开始就在努力实际上只加载登录用户的 UserProfile,似乎我设法使用UserProfile.objects.get(user=request.user)UserProfileForm(request.POST, instance=request.user )但不确定这是否是正确的方法。 Currently my code populates the first_name, last_name and email address in the html form output, but when updating a field and clicking 'Update' button it kind of refreshes but doesn't save to database.目前,我的代码在 html 表单输出中填充 first_name、last_name 和电子邮件地址,但是当更新字段并单击“更新”按钮时,它会刷新但不会保存到数据库。

Is the form.save() not enough or I'm doing something wrong? form.save() 不够用还是我做错了什么?

If you have a suggestion on a much simpler way to do it I'm all ears.如果您有关于更简单的方法的建议,我会全力以赴。 I have also tried looking into UpdateView methods but again all examples I found always had gaps and I couldn't fit it for my purpose.我也试过研究 UpdateView 方法,但我发现的所有例子总是有差距,我无法满足我的目的。 Any benefit in using UpdateView over ModelForm?在 ModelForm 上使用 UpdateView 有什么好处?

Long and winded question maybe, but hopefully you got the problem and hopefully this can help other people who have similar issue.可能是冗长而冗长的问题,但希望您能解决问题,并希望这可以帮助其他有类似问题的人。

def myprofile(request):
    if request.method == 'POST':
        user_profile = UserProfile.objects.get(user=request.user)
        form = UserProfileForm(request.POST, instance=user_profile)
        ...

You need to get the UserProfile instance and pass that one to the UserProfileForm, just like @willem-van-onsem mentioned in the comment.您需要获取 UserProfile 实例并将其传递给 UserProfileForm,就像评论中提到的 @willem-van-onsem 一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM