我将使用表单向DBase添加记录。因此,当request.method ='Post'和form.is_valid时,我需要将数据写入数据库。 我在views.py
中写了这个def makepicturepost(request):
form = PostForm2()
print('View called')
print('Request_method ' + request.method + ' Form.is_valid ' + str(form.is_valid()))
if request.method == 'POST' and form.is_valid():
author = form.author
comment = form.comment
picture = form.picture
newpost = PicturePost(author=author, comment=comment, picture=picture)
newpost.save()
return HttpResponseRedirect('/')
context = {
"form": form
}
return render(request, "makepost.htm", context)
在调用form.is_valid()之后应该检查表单验证,所以我在forms.py中写了一些验证方法
class PostForm2(forms.Form):
author = forms.CharField(max_length=30, widget=forms.TextInput)
comment = forms.CharField(max_length=1500, widget=forms.Textarea)
picture = forms.ImageField()
def clean_author(self):
print('cleaned_author')
author = self.cleaned_data.get('author')
if not author:
raise forms.ValidationError("Autor name shouldn't be blank")
return author
def clean_comment(self):
print('cleaned_comment')
comment = self.cleaned_data.get('comment')
if not comment:
raise forms.ValidationError("Write a pair of lines as a comment")
return comment
def clean_picture(self):
print('cleaned_picture')
picture = self.cleaned_data.get('picture')
print(picture)
return picture
我打算检查图片对象,找出如何检查它只是一个图像。 但我的clean_field方法似乎根本没有调用。这就是我在控制台中所拥有的:
View called
Request_method POST Form.is_valid False
[13/Jun/2017 11:12:38] "POST /post/ HTTP/1.1" 200 788
正如我理解文档,他们应该运行,但他们没有。我哪里错了?
答案 0 :(得分:2)
要实现这一目标,您必须考虑以下几点:
makepost.htm
模板表单应包含enctype="multipart/form-data"
request.FILES
才能访问文件form.cleaned_data['field']
记住这些代码可以得到您想要的结果
def makepicturepost(request):
if request.method == 'POST':
form = PostForm2(request.POST, request.FILES)
if form.is_valid():
author = form.cleaned_data['author']
comment = form.cleaned_data['comment']
picture = form.cleaned_data['picture']
newpost = PicturePost(author=author, comment=comment, picture=picture)
newpost.save()
return HttpResponseRedirect('/')
else:
form = PostForm2()
context = {
'form': form
}
return render(request, 'makepost.htm', context)
答案 1 :(得分:1)
def makepicturepost(request):
form = PostForm2(request.POST)
print('View called')
print('Request_method ' + request.method + ' Form.is_valid ' + str(form.is_valid()))
if request.method == 'POST' and form.is_valid():
author = form.author
comment = form.comment
picture = form.picture
newpost = PicturePost(author=author, comment=comment, picture=picture)
newpost.save()
return HttpResponseRedirect('/')
context = {
"form": form
}
return render(request, "makepost.htm", context)
您没有使用帖子数据来检查PostForm2的干净方法
答案 2 :(得分:0)
1)您需要使用POST数据初始化表单
2)您需要使用form.cleaned_data
def makepicturepost(request):
form = PostForm2(request.POST)
if request.method == 'POST' and form.is_valid():
author = form.cleaned_data['author']
comment = form.cleaned_data['comment']
picture = form.cleaned_data['picture']
newpost = PicturePost(author=author, comment=comment, picture=picture)
newpost.save()
return HttpResponseRedirect('/')
context = {
"form": form
}
return render(request, "makepost.htm", context)
更简单的方法是使用ModelForm并让表单在保存时为您创建一个新实例。
class PostForm2(forms.ModelForm):
class Meta:
model = PicturePost
fields = ['author', 'comment', 'picture']
# I've removed your clean methods as the the form does it for you
# But the message may be different
def makepicturepost(request):
form = PostForm2(request.POST)
if request.method == 'POST' and form.is_valid():
form.save()
return HttpResponseRedirect('/')
context = {
"form": form
}
return render(request, "makepost.htm", context)