您知道如何允许用户在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'),