我正在使用模型表单来扩展用户的个人资料。我希望以前保存的数据在用户决定重写之前显示在表单中。如何使用 django 在模板中执行此操作?
模型
class UserProfile(models.Model):
user = models.ForeignKey(User, primary_key=True)
quote = models.CharField('About', max_length = 200, null=True, blank=True)
website = models.URLField('Personal website/blog', null=True, blank=True)
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ('quote', 'website')
widgets = {
'quote': Textarea(attrs={'cols': 18, 'rows': 7, 'style': "width: 300px;"}),
}
看法
@login_required
def user_profile(request):
user = User.objects.get(pk=request.user.id)
if request.method == 'POST':
upform = UserProfileForm(request.POST)
if upform.is_valid():
up = upform.save(commit=False)
up.user = request.user
up.save()
return HttpResponseRedirect('/accounts/profile')
else:
upform = UserProfileForm()
return render_to_response('reserve/templates/edit_profile.html', locals(), context_instance=RequestContext(request))
模板
{{ upform.as_p}}