Django - Modelform not rendering
我创建了一个表单来更新用户的配置文件,但是当我运行它时,没有任何错误,但是当我尝试打开页面时,表头出现,但没有出现UpdateBioForm。其次,我想知道如何创建一个大型文本框来存储某人的传记。Django - Modelform not rendering
Models.py
class UserProfile(models.Model): user = models.OneToOneField(User)
biography = models.CharField(max_length = 255, default = '')
city = models.CharField(max_length=100, default = '')
website = models.URLField(default='')
image = models.ImageField(upload_to='profile_image', blank=True)
def setdefault(self, default_path='/profile_image/Default.jpg'):
if self.image:
return self.image
return default_path
def __str__(self):
return self.user.username
Forms.Py
class UpdateBioForm(forms.ModelForm): class Meta:
model = UserProfile
fields = (
'biography',
'city',
'website'
)
def save(self, commit=True):
savedBio = super(UpdateBioForm, self).save(commit=False)
savedBio.biography = self.cleaned_data['biography']
savedBio.city = self.cleaned_data['city']
savedBio.website = self.cleaned_data['website']
if commit:
savedBio.save()
return savedBio
Views.py
def update_bio(request): if request.method == 'POST':
form = UpdateBioForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
return redirect('/')
else:
form = UpdateBioForm(instance=request.user)
args = {'form':form}
return render(request, 'accounts/update_bio.html')
urls.py
url(r'^profile/updatebio/$',views.update_bio, name='update_bio'),
update_bio.html
{% extends 'base.html' %} {% block body %}
<div class="container">
<h1>Update Biography</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}
回答:
您没有传递任何情况下你render()
方法 - 定义args
但不要做与该变量什么。将其更改为:
args = {'form':form} return render(request, 'accounts/update_bio.html', context=args) # <-- You're missing context
以上是 Django - Modelform not rendering 的全部内容, 来源链接: utcz.com/qa/261474.html