I am following a tutorial, but this tutorial's django version is different form mine which is 2.0.5. So,I got this issue:
list_of_post() got an unexpected keyword argument 'slug'
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/a-test/
Django Version: 2.0.5
Exception Type: TypeError
Exception Value:
list_of_post() got an unexpected keyword argument 'slug'
Exception Location: /Users/sumeixu/anaconda3/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 126
Python Executable: /Users/sumeixu/anaconda3/bin/python
Python Version: 3.6.3
Python Path:
['/Users/sumeixu/djangotest',
'/Users/sumeixu/anaconda3/lib/python36.zip',
'/Users/sumeixu/anaconda3/lib/python3.6',
'/Users/sumeixu/anaconda3/lib/python3.6/lib-dynload',
'/Users/sumeixu/anaconda3/lib/python3.6/site-packages',
'/Users/sumeixu/anaconda3/lib/python3.6/site-packages/aeosa']
Server time: Thu, 7 Jun 2018 15:00:29 +0000
blog/view.py
def post_detail(request,slug):
post = get_object_or_404(Post,slug=slug)
template = 'blog/post/post_detail.html'
return render(request,template,{'post':post})
post_of_detail.html
{% extends 'blog/post/base.html' %}
{% block title %}{{post.seo_title}}{% endblock %}
{% block content %}
<h2>{{ post.title }}</h2>
<p>Written by {{post.author }} on {{post.published}}</p>
<hr>
{{posts.content}
{% endblock %}
list_of_post.html
{% extends 'blog/post/base.html' %}
{% block title %}List of blog post{% endblock %}
{% block content %}
{% for posts in post %}
<h2><a href="{{posts.get_absolute_url}}">{{ posts.title }}</a></h2>
<p>Written by {{ posts.author }} on {{ posts.published}}</p>
<hr>
{{ posts.content}}
{% endfor %}
{% endblock %}
blog/urls.py
from django.conf.urls import url
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns =[
path('',views.list_of_post,name='list_of_post'),
path('<slug:slug>/',views.list_of_post,name='post_detail')
]
I am sorry if this is too simple. But I am still a programmer beginner. Just need some help. Thank you.
In your urls.py
you write:
from django.conf.urls import url
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns =[
path('',views.list_of_post,name='list_of_post'),
path('<slug:slug>/',views.list_of_post,name='post_detail')
]
But the view should probably be post_detail
, so you should change it to:
from django.conf.urls import url
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns =[
path('',views.list_of_post,name='list_of_post'),
path('<slug:slug>/',views.post_detail,name='post_detail')
]
By linking list_of_post
to the second path(..)
Django will invoke this function with a slug
parameter, but of course the function can not handle this, hence the TypeError
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With