django编辑用户配置文件onetoone字段

时间:2017-10-09 17:40:46

标签: django

您知道如何允许用户在Django中编辑用户模型

但它不适用于onetoone字段。

请告诉我如何允许用户使用onetoone字段编辑用户 我试过但它不起作用

model.py

class userProfile(models.Model):
    userName = models.OneToOneField(User)
    nick = models.CharField(max_length=100)
    agreement = models.BooleanField(default=False)
    profileimage = models.ImageField(upload_to = profileimagepath, blank=True)

    def __unicode__(self):  # __str__
        return unicode(self.userName)

forms.py

class EditProfileForm(UserChangeForm):

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

views.py

def edit_profile(request):
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=request.user)

        if form.is_valid():
            form.save()
            return redirect('/changeprofile')

    else:
        form = EditProfileForm(instance=request.user)
        args = {'form':form}
        return render(request, 'editprofile.html', args)

editprofile.html

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">change</button>

urls.py

url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),

1 个答案:

答案 0 :(得分:0)

要更新通过OneToOne Fields相关的一个表单中的2个模型,您需要使用django-better-forms

中的 MultiModelForm