Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.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/2/django/21.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:如何更新用户扩展模型中的特定字段?_Python_Django - Fatal编程技术网

Python Django:如何更新用户扩展模型中的特定字段?

Python Django:如何更新用户扩展模型中的特定字段?,python,django,Python,Django,我对Django还比较陌生,我对这一点很感兴趣。我一直在尝试创建一个网站,用户可以在其中创建自己的帐户,查看自己和其他成员的个人资料页面 models.py class UserProfileInfo(models.Model): """docstring for ClassName""" user = models.OneToOneField(User) status = models.TextField(blank = True) desc = models.

我对Django还比较陌生,我对这一点很感兴趣。我一直在尝试创建一个网站,用户可以在其中创建自己的帐户,查看自己和其他成员的个人资料页面

models.py

class UserProfileInfo(models.Model):
    """docstring for ClassName"""
    user = models.OneToOneField(User)
    status = models.TextField(blank = True)
    desc = models.TextField(blank = True)
    portfolio_site = models.URLField(blank = True)
    profilepic = models.ImageField(blank = True, null = True, upload_to = 'profile_pics') 
    def __str__(self):
        return self.user.username
forms.py

class UserForm(forms.ModelForm):
    password = forms.CharField(widget =  forms.PasswordInput())

    class Meta():
        model = User
        fields = ('username', 'email', 'password')

class UserProfileInfoForm(forms.ModelForm):
    class Meta():
        model = UserProfileInfo
        fields = ('desc', 'portfolio_site', 'status', 'profilepic')
因此,我希望用户能够通过在文本框中键入内容,从主页更新其状态

index.html(用户可以从中更新其状态的主页)

{%csrf_令牌%}

您保存的是用户,而不是userprofileinfo对象。它是一个单独的模型,因此需要单独保存

profile = user.userprofileinfo
profile.status = mystatus
profile.save()

您保存的是用户,而不是userprofileinfo对象。它是一个单独的模型,因此需要单独保存

profile = user.userprofileinfo
profile.status = mystatus
profile.save()

如果在UserProfileInfo中有更新字段,则获取UserProfileInfo的对象,并按如下所示更新字段

def user_status(request,username):

    if request.method == 'POST':
         mystatus = request.POST.get('status')
         user = User.objects.get(username=username)
         userprofileinfo = user.userprofileinfo
         userprofileinfo.status = mystatus
         userprofileinfo.save()
         return redirect('app5:profilepage',username=username)

如果在UserProfileInfo中有更新字段,则获取UserProfileInfo的对象,并按如下所示更新字段

def user_status(request,username):

    if request.method == 'POST':
         mystatus = request.POST.get('status')
         user = User.objects.get(username=username)
         userprofileinfo = user.userprofileinfo
         userprofileinfo.status = mystatus
         userprofileinfo.save()
         return redirect('app5:profilepage',username=username)

你有什么错误?我没有任何错误。只是更改没有发生在管理员或用户配置文件页面中。您会得到什么错误?我没有得到任何错误。只是更改没有发生在管理员或用户配置文件页面。哦,我应该注意到这一点。非常感谢你!这很有效。哦,我应该注意到的。非常感谢你!成功了。