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!
CodePudding user response:
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
class Comments(models.Model):
listing = models.ForeignKey(Listings, on_delete=models.CASCADE) # for one-to-many relation
comment = models.CharField(max_length=500)
and while saving your form pass listing object and while rendering filter it with current listing
@login_required(login_url='login')
def listing(request, id):
listing = Listings.objects.get(id=id)
comment_obj = Comments.objects.filter(listing=listing)
form = CommentForm() # you have to initialize form properly
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.listing = listing
comment.save()
else:
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj
})
return render(request, "auctions/listing.html",{
"auction_listing": listing,
"form": form,
"comments": comment_obj
})
I'll suggest to look at ForeignKey.related_name
CodePudding user response:
It would be helpful to have more information regarding your Listing model class as well as your CommentForm. There may be something in your CommentForm class that is causing it to be associated to all of the Listings. It also sounds like you have a ForiegnKey in your Listing model that points to your comment. It would be better to do as Ankit Tiwari suggested and link your comment to your listing, so that multiple comments can point to the same listing.