The codes (below) must enable a user to comment on a post/page. Thus, the comment is linked to that page. But after the submit event, the comment is not saved, hence nothing is displayed except the comments from the Admin page.
Function that display the page and the comment in view.py
def list_page(request, list_id):
if request.user.is_authenticated:
list_auction = Auction_listings.objects.get(id=list_id)
categories = Category.objects.get(id = list_id)
return render(request, "auctions/auc_details.html", {
"detail": list_auction,
"cats":categories,
"user": request.user,
"comments": list_auction.comment.all(),
})
else:
list_auction = Auction_listings.objects.get(id =list_id)
return render(request, "auctions/auc_details.html", {
"detail":list_auction
})
comment function in view.py
def comment(request, list_id):
if request.user.is_authenticated:
list_auction = get_object_or_404(Auction_listings, pk=list_id)
if request.method == "POST":
comment_form = forms.Create_comment(request.POST)
if comment_form.is_valid():
com_t = comment_form.save(commit=False)
com_t.comment = list_auction
com_t.comment_by = request.user
com_t.save()
print(com_t)
return HttpResponseRedirect(reverse("list_page", args=(list_auction.id,)))
return render (request,"auctions/auc_details.html", {
"detail": list_auction,
"user": request.user,
"comments": list_auction.comment.all(),
})
Route in urls.py
path("<int:list_id>/comment", views.comment, name="comment"),
Form class in form.py
class Create_comment(forms.ModelForm):
class Meta:
model = models.Comment
fields = ['comment']
Model class in models.py Comment model
class Comment(models.Model):
comment_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="commentor", blank=False)
comment_on = models.ForeignKey( Auction_listings, related_name="comment", on_delete=models.CASCADE)
comment = models.CharField(max_length=600)
comment_date_published = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.comment}"
Page model (auction_listings
class Auction_listings(models.Model):
auc_title = models.CharField(max_length=50)
auc_details = models.CharField(max_length=250)
auc_price = models.IntegerField()
auc_date_published = models.DateTimeField(auto_now_add=True)
auc_created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name="creator", blank=True)
auc_image = models.ImageField(default='rose.jpg', blank=True)
auctions = models.ManyToManyField(Category, blank= True, related_name="category")
def __str__(self):
return f"{self.auc_title}"
html page
<form action="{% url 'comment' detail.id %}" method="POST">
{% csrf_token %}
<input type="text" name="text">
<input type="hidden" name="auction_id" value="{{detail.id}}">
<button type=" submit ">Create</button>
</form>
I tried to change the route to look like:
path("/comment/<int:list_id>", views.comment, name="comment"),
Tried But that didn't change anything.
CodePudding user response:
If you expect the comment to attach to list_auction there should be a connection between them
com_t.comment_on = list_auction
OR
com_t.comment_on_id = list_auction.id
in your case:
if comment_form.is_valid():
com_t = comment_form.save(commit=False)
com_t.comment = 'the text of comment'
com_t.comment_on = list_auction
com_t.comment_by = request.user
com_t.save()
print(com_t)
return HttpResponseRedirect(reverse("list_page", args=(list_auction.id,)))
NOTE: For better debugging, check your database to see how the data is stored
CodePudding user response:
1. Call the Create_comment from form.py
def list_page(request, list_id):
if request.user.is_authenticated:
list_auction = Auction_listings.objects.get(id=list_id)
categories = Category.objects.get(id = list_id)
comment_form = forms.Create_comment()
return render(request, "auctions/auc_details.html", {
"detail": list_auction,
"cats":categories,
"user": request.user,
"comments": list_auction.comment.all(),
"com_form":comment_form,
})
else:
list_auction = Auction_listings.objects.get(id =list_id)
return render(request, "auctions/auc_details.html", {
"detail":list_auction
})
2 Call the ""com_form"" on the template.
<form action="{% url 'comment' detail.id %}" method="POST">
{% csrf_token %}
{{com_form}}
<button type=" submit ">Create</button>
</form>