'How to fix " TypeError: post_details() got an unexpected keyword argument 'slug' "
I'm creating my blog with django, and trying to make posts urls in slug way, after I created the models and called it in views it showed me this error: [TypeError at /posts/post/why-i-love-python-django/,
post_details() got an unexpected keyword argument 'slug']
- My files are in these links:
- [1]: https://pastebin.com/VuiPn7Vf => posts/views.py,
- [2]: https://pastebin.com/HjTK5Ugz => posts/models.py,
- [3]: https://pastebin.com/03gn3mbK => posts/urls.py,
- [4]: https://pastebin.com/xAVc3ppe => templates/post.html,
- [5]: https://pastebin.com/AzMQPxrv => templates/base.html
I tried to change "id" in [posts/views.py line:102] and made it "slug", also edited it in [line:104]
in [posts/view.py line: 102]:
def post_details(request, id):
post_list = Post.objects.active()
post = get_object_or_404(Post, id=id)
if post.publish > timezone.now().date() or post.draft:
if not request.user.is_authenticated or not request.user.is_superuser:
raise Http404
share_string = quote_plus(post.content)
initial_data = {
"content_type": post.get_content_type,
"object_id": post.id,
}
# post read time
read_time = get_read_time(post.content)
# print(type(read_time))
form = CommentForm(request.POST or None, initial=initial_data)
if form.is_valid():
c_type = form.cleaned_data.get('content_type')
content_type = ContentType.objects.get(model=c_type)
obj_id = form.cleaned_data.get('object_id')
content_data = form.cleaned_data.get('content')
parent_obj = None
try:
parent_id = int(request.POST.get('parent_id'))
except:
parent_id = None
if parent_id:
parent_qs = Comment.objects.filter(id=parent_id)
if parent_qs.exists() and parent_qs.count() == 1:
parent_obj = parent_qs.first()
new_comment, created = Comment.objects.get_or_create(
user = request.user,
content_type = content_type,
object_id = obj_id,
content = content_data,
parent = parent_obj,
)
return HttpResponseRedirect(new_comment.content_object.get_absolute_url())
comments = post.comments # Comment.objects.filter_by_instance(post) # without @property
context = {
'post': post,
'share_string': share_string,
'comments': comments,
'comment_form': form,
'read_time': read_time,
}
return render(request, 'post.html', context=context)
I expected the post slug to be created and added to the urls correctly
Solution 1:[1]
Your post_details
function takes request
(which all Django views should take) and id
parameters, but in urls.py
you are extracting parameter named slug
. Your function should take that param instead of id
.
Solution 2:[2]
Solution: this problem solved by editing the [post.html] at line:53
<!-- path: {% url 'posts:post' id=post.id %} <br> -->
and make it an acceptable "django comment" like : {# #} because server can't render html comments, so I edited to be like this:
{# path: {% url 'posts:post' id=post.id %} #}<!-- <br> -->
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | GwynBleidD |
Solution 2 | Belal Abdulnasser |