我想为我的帖子保存评论,但由于某些原因,视图返回(如果我输入了错误的验证码):
视图MyProject.views.comment_new没有返回HttpResponse 宾语。它返回None。
任何想法可能是这种行为的原因吗? 遗憾的是,这是唯一一个似乎完全忽略了我的验证码的地方/形式
views.py
def comment_new(request, pk):
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
post = get_object_or_404(Post, pk=pk)
comment = form.save(commit=False)
comment.author = request.user
comment.published_date = timezone.now()
comment.post = post
comment.save()
return redirect('post_detail', pk=comment.post.pk)
else:
form = CommentForm()
return render(request, 'MyProject/comment_new.html', {'form': form})
urls.py:
url(r'^message/(?P<pk>\d+)/free/comment/new/$', auth_required(MyProject_views.comment_new), name='comment_new'),
models.py
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=40)
content = models.TextField(verbose_name="Post Content", max_length=5000)
....
class Comment(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField(max_length=500)
published_date = models.DateField(auto_now_add=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
.....
template.html:
<body>
<h1 class="center">Create new comment</h1>
<hr class="hr-style">
<div>
<form action="" method="post" class="class-two-box">
{% csrf_token %}
<table>
{{ form.as_p }}
{{ field.help_text }}
</table>
<hr>
<button class="btn btn-success" type="submit">Publish Comment</button>
</form>
</div>
</body>
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['content']
captcha = CaptchaField()
def __init__(self, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
self.fields['content'].widget.attrs.update({'class': 'class-two-input-fields'})
答案 0 :(得分:1)
首先,它将返回错误:
没有返回HttpResponse对象
因为如果您的form.is_valid()
返回False
,则没有继续执行的路径,因此返回了None
。
您需要这样做:
def comment_new(request, pk):
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
post = get_object_or_404(Post, pk=pk)
comment = form.save(commit=False)
comment.author = request.user
comment.published_date = timezone.now()
comment.post = post
comment.save()
return redirect('post_detail', pk=comment.post.pk)
else: # If for is NOT valid:
form = CommentForm(request.POST)
return render(request, 'MyProject/comment_new.html', {'form': form})
else:
form = CommentForm()
return render(request, 'MyProject/comment_new.html', {'form': form})
或者为了避免代码在视觉上重复(保持“ DRY”):
def comment_new(request, pk):
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
post = get_object_or_404(Post, pk=pk)
comment = form.save(commit=False)
comment.author = request.user
comment.published_date = timezone.now()
comment.post = post
comment.save()
return redirect('post_detail', pk=comment.post.pk)
else:
form = CommentForm()
# Note the indentation (this code will be executed both if NOT POST
# and if form is not valid.
return render(request, 'MyProject/comment_new.html', {'form': form})
答案 1 :(得分:1)
此行
def failTryUnit(x: Unit): Try[Int] = Try {
println(x)
1 / 0
}
应该有一个if form.is_valid():
分支,否则它将返回else