0

大家好,我收到此错误: 在此处输入图像描述

这是我的主要网址:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/',include('blog.urls',namespace='blog')),
]

我的博客/url.py:

app_name = 'blog'

urlpatterns = [
    path('',views.PostListView.as_view(), name='post_list'),
    path('<int:year>/<int:month>/<int:day>/<int:post>',views.post_detail,name='post_detail'),
]

视图.py:

class PostListView(ListView):
    queryset = Post.published.all()
    context_object_name = 'posts'
    paginate_by = 3
    template_name = 'blog/post/list.html'

    def post_list(request):
        posts = Post.published.all()
        return render(request, 'blog/post/list.html', {'posts': posts})

    def post_detail(request, year, month, day, post):
        post = get_object_or_404(Post, slug=post, 
        status='published',publish__year=year,publish__month=month,publish__day=day)
        return render(request, 'blog/post/detail.html', {'post': post})

列表.html:

{% extends "blog/base.html" %}

{% block title %}My Blog{% endblock %}

{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
    <h2><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
    <p class="date">Published {{ post.publish }} by {{ post.author }}</p>
    {{ post.body|truncatewords:30|linebreaks }}
{% endfor %}

{% include "pagination.html" with page=page_obj %}
{% endblock %}

我不知道代码有什么问题。我需要一些帮助。

4

1 回答 1

1

错误可能在您的 URL 模式中 - URL 模式中的最后一个关键字参数post_detail应该是slug,而不是int

app_name = 'blog'

urlpatterns = [
    path('',views.PostListView.as_view(), name='post_list'),
    path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
]

此外,确保以正斜杠结束 URL 模式,以保持一致性。

于 2018-02-06T20:24:23.747 回答