我遇到了一个非常奇怪的问题,post方法相互提交:
我用下列表格表示:
test1
,test2
,test3
,test4
,test5
,test6
后续并提交了
但显示了test2
,test4
,test6
,
article_detail.html
<div>
{% for comment in comments %}
<p>{{ comment.comment|linebreaks }}</p>
{% endfor %}
</div>
<div>
<h4>Comments</h4>
<form action="/article/comment/create/{{ article.id }}" method='post'>
{% csrf_token %}
<textarea class="form-control" rows="5" value={{ form.comment.value }} name='comment'></textarea>
<br>
<button type="submit" class="btn btn-primary">Post Your Comment</button>
</form>
</div>
<div><!--/class="col-xs-8 col-md-8">-->
</div><!-- row -->
models.py:
class Comment(models.Model):
STATUS = (
(0, 'normal'),
(-1, 'deleted'),
)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
comment = models.CharField(max_length=5000) # set the widget
status = models.IntegerField(choices=STATUS)
date_created = models.DateTimeField(default=datetime.now)
date_updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.comment
forms.py:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['comment']
views.py
class CommentCreateView(View):
template_name = "article/article_detail.html"
def get(self, request, pk):
return redirect(f"/article/detail/{ pk }")
def post(self, request, pk):
self.article = Article.objects.get(id=pk)
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
print(f"Comment: {form.cleaned_data}")
comment.owner = request.user
comment.article = self.article
comment.status = 0
comment.save()
return redirect(f"/article/detail/{ pk }")
else:
comments = (Comment.objects
.filter(article=self.article, status=0)
)
context = {'article': self.article,
'comments':comments,
"form": form}
return render(request, self.template_name, context)
问题可能是什么?