Home > Software engineering >  Django error : NoReverseMatch at /Post/8 Reverse for 'comments' with arguments '(
Django error : NoReverseMatch at /Post/8 Reverse for 'comments' with arguments '(

Time:11-06

I am working on my project I faced a problem with "COMMENT": I add a comment as a section when the user clicks "view" button to see the post from the home page.

Django error : NoReverseMatch at /Post/8 Reverse for 'comments' with arguments '('',)' not found. 1 pattern(s) tried: ['Post/(?P[0-9] )$']

views.py file

def viewList(request, id):
item = Post.objects.get(id=id)
context = {
    'item': item,
    'comment_form': comment(),
    'comments': item.get_comments.all(),
}
return render(request, 'auctions/item.html', context)

@login_required
def comments(request, id):
     listing = Post.objects.get(id=id)
     form = comment(request.PSOT)
     newComment = form.save(commit=False)
     newComment.user = request.user
     newComment.listing = listing
     newComment.save()
return HttpResponseRedirect(reverse("listing", {'id': id}))

models.py file

class Post(models.Model):

# data fields
title = models.CharField(max_length=64)
textarea = models.TextField()

# bid
price = models.FloatField(default=0)
currentBid = models.FloatField(blank=True, null=True)

imageurl = models.CharField(max_length=255, null=True, blank=True)
category = models.ForeignKey(
    Category, on_delete=models.CASCADE, default="No Category Yet!", null=True,  blank=True)

creator = models.ForeignKey(User, on_delete=models.PROTECT)
watchers = models.ManyToManyField(
    User, blank=True, related_name='watched_list')
date = models.DateTimeField(auto_now_add=True)

# for activated the Category
activate = models.BooleanField(default=True)

def __str__(self):
    return f"{self.title} | {self.textarea} |  {self.date.strftime('%B %d %Y')}"

class Comment(models.Model):

body = models.CharField(max_length=100)
createdDate = models.DateTimeField(default=timezone.now)

# link to the post model
auction = models.ForeignKey(
    Post, on_delete=models.CASCADE, related_name="get_comments")

user = models.ForeignKey(User, on_delete=models.CASCADE)

status = models.BooleanField(default=True)

def __str__(self):
    return self.createdDate.strftime('%B %d %Y')

HTML file

<!-- Comments -->
    <div class="comments">
        <p>Add a comment:</p>
        <div class="row">
            <div class="col-6">
                <form action="{% url 'comments' listing.id %}" method="post">
                    {% csrf_token %}
                    <div class="input-group">
                        {{ comment_form }}
                    </div>
                        <input type="submit" value="save" class="btn btn-outline-dark btn-sm m-1"/>
                </form>
            </div>
        {% for comment in comments %}
            <div class="col-4">
                Comments:
                <h4>Name: {{comment.user}}</h4>
                <h4>Content: {{comment.body}}</h4>
                <h4>Date: {{comment.createdDate}}</h4>
            </div>
        {% endfor %}
        </div>
    </div>

url.py

from django.urls import path 
from . import views


urlpatterns = [
     path("", views.index, name="index"),
     path("login", views.login_view, name="login"),
     path("logout", views.logout_view, name="logout"),
     path("register", views.register, name="register"),
     path("category/", views.category, name="category"),
     path("Post", views.createList, name="createList"),
      #comment url
     path("Post/<str:id>", views.viewList, name="viewList"),
     path("Post/<int:id>", views.comments, name="comments"),
]

CodePudding user response:

You passed the Post object as item to the template, hence you should resolve the URL with:

<!--        use item not listing ↓  -->
<form action="{% url 'comments' item.id %}" method="post"></form>

Your view to create comments also has some mistakes: you should use request.POST, not request.PSOT:

from django.shortcuts import redirect

@login_required
def comments(request, id):
    if request.method == 'POST'
         form = comment(request.POST, request.FILES)
         if form.is_valid():
             form.instance.user = request.user
             form.instance.auction_id = id
             form.save()
    return redirect('viewList', id=id)

Furthermore your comment form should inherit from ModelForm and not Form:

class comment(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ('content',)
        widgets = {
            'content': forms.Textarea(attrs={"class": "form-control"})
        }

finally your urls.py has two (fully) overlapping patterns, you should use a different URL to add the given comment:

urlpatterns = [
     # …
     #comment url
     path('Post/<int:id>', views.viewList, name='viewList'),
     path('Post/<int:id>/add', views.comments, name='comments'),
]

Note: Usually a Form or a ModelForm ends with a …Form suffix, to avoid collisions with the name of the model, and to make it clear that we are working with a form. Therefore it might be better to use CommentForm instead of comment.

  • Related