Lily H.
Lily H.

Reputation: 194

Reverse for 'post-detail' with arguments '('',)' not found

In django I have issues to understand the changes while handling the data transfer to templates with the {{}} ...it seem to change all the time. Why ? Here my code gives me the error: Reverse for 'post-detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P[0-9]+)$']

My view seem alright so is my template. And I don't know why I should change my url as it was working fine..

### app views
class PostDetailView(LoginRequiredMixin, DetailView):
    model = Post
    template_name = 'blog/post_detail.html'


    def get_context_data(self,*arg, **kwargs):
        context = super().get_context_data(**kwargs)
        form = CommentForm()
        context['form'] = form
        return context

    def post(self, request,*arg, **kwargs):
        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                form.save()
        else:
            form = CommentForm()
        context ={
            'form':form
        }
        return render(request, self.template_name, context)

templates :

 <form method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {{ form | crispy }}
       <button class="btn btn-primary" type="submit" > submit       </button>
       <input value="bad word" type="submit" onclick="{% url 'post-detail' post.id %}">
      </form>

urls :

path('post/<int:pk>', PostDetailView.as_view(), name='post-detail'),

I tried every help post online but without success. I just want to be able to post comments under the blog post... If someone knows which direction I should take that would be great!

Upvotes: 0

Views: 517

Answers (1)

hassanzadeh.sd
hassanzadeh.sd

Reputation: 3471

you can change your url like this :

path('post/<int:pk>', PostDetailView.as_view(), name='post-detail')

or if you want slug with other field use instead of "pk"

like this :

<slug:title>

Upvotes: 1

Related Questions