简体   繁体   English

Django更新用户个人资料

[英]Django update user profile

I am facing a lot of problems to update a user profile as customer. 我在更新用户资料作为客户时遇到很多问题。

Problems: 问题:

  • No errors showing 无错误显示
  • Only Email, Address and Birthday are updating 仅电子邮件,地址和生日在更新
  • Want to update First name and last name in user 想要更新用户的名字和姓氏

if there is any short way to this kind of update profile, please let me know or please try to help with this code. 如果有这种更新配置文件的简短方法,请让我知道,或者尝试帮助提供此代码。 Thank you. 谢谢。

models.py models.py

from django.db import models
from django.contrib.auth.models import User


class SignupUser(models.Model):
    user = models.OneToOneField(User)
    address = models.CharField(blank=False, max_length=200)
    birthday = models.DateTimeField('Birthday')

    def __str__(self):
        return self.address


User.profile = property(lambda u: SignupUser.objects.get_or_create(user=u)[0])

forms.py 表格

from django import forms
from django.contrib.auth.models import User
from signup.models import SignupUser

class UpdateProfile(forms.ModelForm):
    class Meta:
        model = SignupUser
        fields = ['address', 'birthday']


class UpdateUser(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'email']


    def clean_email(self):
        email = self.cleaned_data['email']
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError('This is not your email')

        return email

views.py views.py

def editprofile(request):
    if request.method == 'POST':
        profileup = UpdateProfile(request.POST or None, instance=request.user.profile)
        if profileup.is_valid():
            profileup.save()
            userup = UpdateUser(request.POST or None, instance=request.user)
            if userup.is_valid():
                user = userup.save(commit=False)
                user.first_name == request.POST['first_name']
                user.save()
            return HttpResponseRedirect('/authority/editprofile/')
    else:
        user = request.user
        profile = user.profile
        userup = UpdateUser(instance=user)
        profileup = UpdateProfile(instance=profile)

    args = {
        'userup': userup,
        'profileup': profileup, 
    }
    return render(request, 'auth/editprofile.html', args)

Try add else clause after if userup.is_valid() to get more info for the error. 请尝试在if userup.is_valid()之后添加else子句,以获取有关该错误的更多信息。 But i suggest you use class based view UpdateView 但我建议您使用基于类的视图UpdateView

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

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