我如何编辑一个现有评论,当一个用户评论后,用户可以编辑他/她的评论。
- 模型.py*
class Comments(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
commented_image = models.ForeignKey(Image, ...)
comment_post = models.TextField()
- 网址.py*
path('comments/<id>/', comments, name='comments'),
path('comments/<int:id>/<int:comment_id>', comments, name='comments')
- 查看次数.py*
def comments(request, id, comment_id=None):
post = get_object_or_404(Image, id=id)
if request.method == 'POST':
if comment_id:
edit_form = CommentForm(#codes here)
else:
edit_form = CommentForm(data=request.POST)
form = CommentForm(request.POST)
if form.is_form():
comment = form.save(commit=False)
comment.user = request.user
comment.commented_image = post
comment.save()
return redirect(...)
2条答案
按热度按时间wyyhbhjk1#
您必须在更新函数中传递注解ID,如下所示:
并执行以下操作
UPDATE:要使同一视图句柄同时创建和更新,请添加指向同一视图的新URL(并将其放置在原始视图下):
并更新视图,如下所示:
在模板中:如果此表单用于更新:使用
<form method="POST" action="{% url 'comment_update' post.id comment.id %}">
如果是创建表单,只需用途:
<form method="POST" action="{% url 'comment_create' post.id %}">
0wi1tuuw2#
你能提供你的网页截图吗?
你为什么要传递图片?2我猜应该是你ID,是你的发帖请求。