RuntimeError while creating comments api in django

Submitted 3 years, 9 months ago
Ticket #46
Views 251
Language/Framework Django
Priority High
Status Closed

Hello everyone, i was creating api for comments in django and i submit comment then i got runtime error and no comment saved in django administration , can anyone help me to solve this? I have also try putting slash but it did not work.

models.py

class Feed(models.Model):

title = models.CharField(max_length=100, default='')
category = models.ForeignKey(Category, null=True, on_delete=models.PROTECT)
description = models.TextField(default='')
author = models.CharField(max_length=50)
tags = models.ManyToManyField(Tag)
price = models.IntegerField(default=0)
date = models.DateTimeField(auto_now=True)
slug = models.SlugField(blank=True, default='')

def __str__(self):
return f"Author:{self.author},Title: {self.title},Date: {self.date}"

def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Feed, self).save()

def get_absolute_url(self):
return reverse('detail', args=[str(self.slug)])


class FeedComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
feed = models.ForeignKey(Feed, on_delete=models.CASCADE)
parent = models.ForeignKey('self',on_delete=models.CASCADE,null=True)
timestamp = models.DateTimeField(default= now)

def __str__(self):
return self.comment[0:13]+ "..." + "by " + self.user.email

***************************************************

urls.py

urlpatterns = [
# API to post comment
path('feedComment', views.feedComment, name="feedComment"),

path('', views.index, name="Home"),
path('find-jobs/', views.findjobs, name="find-job"),
path('feed/<str:slug>/', views.detail, name="detail")

]

**************************************************

detail.html

<form action="/feed/feedComment" method="post">
{%csrf_token%}
<input type="text" name="comment" placeholder="enter comment here">
<input type="hidden" name="feedId" value="{{feed.sno}}" >
<input type="submit" value="Submit">
</form>

*********************************************************

Views.py

def detail(request, slug):
feed = get_object_or_404(Feed, slug=slug)
comments = FeedComment.objects.filter(feed=feed)
return render(request, 'main/detail.html', {'feed': feed,'comments':comments})

def feedComment(request):
if request.method == "POST":
comment = request.POST.get("comment")
user= request.user
feedId = request.POST.get("feedId")
feed = Feed.objects.get(sno=feedId)

comment = FeedComment(comment=comment,
user=user,
feed=feed)
comment.save()
messages.success(request,"Your comment has been posted successfully.")
return redirect(f"/feed/{feed.slug}")

****************************************************************

error:

Submitted on Jul 20, 20
add a comment

1 Answer

@Swastikshrestha0001 in future kindly use the code formatter when you submit the ticket, then it would help other techions will grab the code and help you easily.

Submitted 3 years, 9 months ago

how about adding form action="/feed/feedComment/" in your html form ?. It should work.

- sunil 3 years, 9 months ago

@sunil I am sorry for bad code format , i am new to teckiy. I add form action="/feed/feedComment/" but it say page not found.

- swastikshrestha0001 3 years, 9 months ago

I think you need to change the url pattern to feedComment/ in urls.py and then remove the /feed/ from form action

- bhavana 3 years, 9 months ago

@Swastikshrestha0001 is this issue fixed?

- sunil 3 years, 9 months ago

@Bhavana i try as you say but it didnot work.

- swastikshrestha0001 3 years, 9 months ago

@sunil this issue is not fixed yet.

- swastikshrestha0001 3 years, 9 months ago

can you show me the form and urls code. It should work if you follow the Bhavana's approach.

- sunil 3 years, 9 months ago

urls.py ``` urlpatterns = [

path('feedComment/', views.feedComment, name="feedComment"),
path('', views.index, name="Home"),
path('find-jobs/', views.findjobs, name="find-job")

] ```

- swastikshrestha0001 3 years, 9 months ago

form code:

<form action="/feedComment" method="post"> {%csrf_token%} <input type="text" name="comment" placeholder="enter comment here"> <input type="hidden" name="feedId" value="{{feed.sno}}" > <input type="submit" value="Submit"> </form>

- swastikshrestha0001 3 years, 9 months ago

@sunil am i doing anything wrong , correct me if i am wrong.

- swastikshrestha0001 3 years, 9 months ago

@sunil if this is not solved can you tell me how to handle live comments in django or share any blog post link where can i find how to do that?

- swastikshrestha0001 3 years, 9 months ago

Step 1:
Your form action URL should be match for one of the pattern in URLS.py in url.py path('feedComment/', views.feedComment, name="feedComment"), so you need to use the same url in form also
Step 2: <form action="/feedComment" method="post"> {%csrf_token%} <input type="text" name="comment" placeholder="enter comment here"> <input type="hidden" name="feedId" value="{{feed.sno}}" > <input type="submit" value="Submit"> </form>

- sunil 3 years, 9 months ago

@Swastikshrestha0001 if the issue is closed , then can you close from your end.

- bhavana 3 years, 8 months ago


Latest Blogs