*def add_comment(request, slug):*
*post = get_object_or_404(Post, slug=slug)*
* if request.method == 'POST':*
* form = CommentForm(request.POST)*
* if form.is_valid():*
* comment = form.save(commit=False)*
* comment.post = post*
* comment.save()*
* return redirect('blog:post_detail', slug=post.slug)*
* else:*
* form = CommentForm()*
* template = 'blog/add-comment.html'*
* context = {*
* 'form': form*
* }*
* return render(request, template, context)*
Check this view, it has an argument called slug and from your url you are
passing parameter to that slug argument i.e path('post/<int:slug>/comment/',
views.add_comment, name='add-comment')
In this case slug field will contain the id of the post i believe,
If you check this *post = get_object_or_404(Post, slug=slug), **slug field
contains the id but your post model does not an attribute called slug so
what you should really do is*
*post = get_object_or_404(Post, id=slug) **or you can rename the url as *
path('post/<int:id>/comment/', views.add_comment, name='add-comment') and
your view as
*def add_comment(request, id):*
*post = get_object_or_404(Post, id=id)*
* if request.method == 'POST':*
* form = CommentForm(request.POST)*
* if form.is_valid():*
* comment = form.save(commit=False)*
* comment.post = post*
* comment.save()*
* return redirect('blog:post_detail', slug=post.slug)*
* else:*
* form = CommentForm()*
* template = 'blog/add-comment.html'*
* context = {*
* 'form': form*
* }*
* return render(request, template, context)*
On Fri, 12 Oct 2018 at 12:56, Robert CR <[email protected]> wrote:
> If i do that i get another error:
>
> [image: Udklip2.PNG]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/769a8b8a-53d8-43fe-9caa-908cc6fc0204%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/769a8b8a-53d8-43fe-9caa-908cc6fc0204%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CAHfehoXQbi948m6-NOgeNvEUj4AWTZxcjjt0wShEO7-ajfgeFw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.