对 'post_detail' 进行反转,参数为 '('chempion',)' 未找到。尝试了 1 种模式:['(?P<category_slug>
Reverse for 'post_detail' with arguments '('chempion',)' not found. 1 pattern(s) tried: ['(?P<category_slug>
'post_detail' 的反转,未找到参数“('chempion',)”。尝试了 1 种模式:['(?P[-a-zA-Z0-9_]+)/(?P[-a-zA-Z0-9_]+)/$' ]
当我向模板添加函数和视图时,我发现了这个错误
view.py
def post_detail(request, category_slug, slug):
post = get_object_or_404(Post, slug=slug)
try:
next_post = post.get_next_by_date_added()
except Post.DoesNotExist:
next_post = None
try:
previous_post = post.get_previous_by_date_added()
except Post.DoesNotExist:
previous_post = None
context = {
'post': post,
'next_post': next_post,
'previous_post': previous_post
}
return render(request, 'post_detail.html', context)
urls.py
path('<slug:category_slug>/<slug:slug>/', post_detail, name='post_detail'),
path('<slug:slug>/', category_detail, name='category_detail'),
post detail.html
{% if next_post %}
<a href="{% url 'post_detail' next_post.slug %}">Next</a>
{% else %}
This is the last post!
{% endif %}
post_detail
视图需要两个 slug:一个用于类别,一个用于 post。
例如,如果您的 Post
模型有一个 ForeignKey
到 Category
模型,您可以通过以下方式引用它:
<a href="{% url 'post_detail' <b>next_post.category.slug</b> next_post.slug %}">Next</a>
在您看来,您可能想要检查类别 的 slug 和 post 的 ,所以:
def post_detail(request, category_slug, slug):
post = get_object_or_404(Post<b>, category__slug=category_slug</b>, slug=slug)
# …
'post_detail' 的反转,未找到参数“('chempion',)”。尝试了 1 种模式:['(?P
当我向模板添加函数和视图时,我发现了这个错误
view.py
def post_detail(request, category_slug, slug):
post = get_object_or_404(Post, slug=slug)
try:
next_post = post.get_next_by_date_added()
except Post.DoesNotExist:
next_post = None
try:
previous_post = post.get_previous_by_date_added()
except Post.DoesNotExist:
previous_post = None
context = {
'post': post,
'next_post': next_post,
'previous_post': previous_post
}
return render(request, 'post_detail.html', context)
urls.py
path('<slug:category_slug>/<slug:slug>/', post_detail, name='post_detail'),
path('<slug:slug>/', category_detail, name='category_detail'),
post detail.html
{% if next_post %}
<a href="{% url 'post_detail' next_post.slug %}">Next</a>
{% else %}
This is the last post!
{% endif %}
post_detail
视图需要两个 slug:一个用于类别,一个用于 post。
例如,如果您的 Post
模型有一个 ForeignKey
到 Category
模型,您可以通过以下方式引用它:
<a href="{% url 'post_detail' <b>next_post.category.slug</b> next_post.slug %}">Next</a>
在您看来,您可能想要检查类别 的 slug 和 post 的 ,所以:
def post_detail(request, category_slug, slug):
post = get_object_or_404(Post<b>, category__slug=category_slug</b>, slug=slug)
# …