Django模型中是否有任何字段,用户没有填充,但是计算机可以填写? (CS50项目2)
我创建了一个Django表单,该表格可为列表创建并保存评论。我希望该评论仅保存到对其进行评论的列表中(现在它保存到所有列表中)。有什么方法可以告诉网页要将其保存到哪个列表以及哪个用户写了评论?
Models.py
class Comments(models.Model):
comment = models.CharField(max_length=500)
Views.py
@login_required(login_url='login')
def listing(request, id):
listing = Listings.objects.get(id=id)
form = CommentForm
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save()
comment.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": Comments.objects.all()
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": Comments.objects.all()
})
HTML
<img src ="{{ auction_listing.image }}" style = "height: 10%; width: 10%;">
<h4 class = "text">{{ auction_listing.title }}</h4>
<h6>Description: {{ auction_listing.description }}</h6>
<h6>Category: {{ auction_listing.category }}</h6>
<h6>Price: ${{ auction_listing.bid }}</h6>
<form action = "{% url 'listing' auction_listing.id %}" method = "POST">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Save">
</form>
{% for comment in comments %}
<h6> {{ comment.comment }} </h6>
{% endfor %}
感谢您的所有帮助!
这是我现在尝试保存新评论时收到的错误。让我知道您是否需要查看评论表格。
IntegrityError at /listing/1/
NOT NULL constraint failed: auctions_comments.listing_id
Request Method: POST
Request URL: http://127.0.0.1:8000/listing/1/
Django Version: 3.2.5
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: auctions_comments.listing_id
I have created a django form that creates and saves comments to listings. I want the comment to only save to the listing that it is commented to (right now it saves to all the listings). Is there any way to tell the webpage which listing to save it to and which user wrote the comment?
models.py
class Comments(models.Model):
comment = models.CharField(max_length=500)
views.py
@login_required(login_url='login')
def listing(request, id):
listing = Listings.objects.get(id=id)
form = CommentForm
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save()
comment.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": Comments.objects.all()
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": Comments.objects.all()
})
html
<img src ="{{ auction_listing.image }}" style = "height: 10%; width: 10%;">
<h4 class = "text">{{ auction_listing.title }}</h4>
<h6>Description: {{ auction_listing.description }}</h6>
<h6>Category: {{ auction_listing.category }}</h6>
<h6>Price: ${{ auction_listing.bid }}</h6>
<form action = "{% url 'listing' auction_listing.id %}" method = "POST">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Save">
</form>
{% for comment in comments %}
<h6> {{ comment.comment }} </h6>
{% endfor %}
Thanks for all the help!
This is the error I am now receiving when I try to save a new comment. Let me know if you need to see the comment form.
IntegrityError at /listing/1/
NOT NULL constraint failed: auctions_comments.listing_id
Request Method: POST
Request URL: http://127.0.0.1:8000/listing/1/
Django Version: 3.2.5
Exception Type: IntegrityError
Exception Value:
NOT NULL constraint failed: auctions_comments.listing_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望该评论仅保存到列表中,该列表将其评论给 - ,以便将评论保存到当前或特定的清单中,您可以在评论和评论之间创建关系清单。
更新您的评论模型
,并在保存您的表格通过列表对象时,并在使用当前列表进行过滤时,
我建议您查看
foreferkey.related_name_name
I want the comment to only save to the listing that it is commented to - In order to save comments to current or specific listing you've to create a relation between comment and listing.
Update your comment model like this
and while saving your form pass listing object and while rendering filter it with current listing
I'll suggest to look at
ForeignKey.related_name