0

Hello guys I am receiving this error : enter image description here

This my main URL:

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

My 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'),
]

view.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})

list.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 %}

I don't know what is the problem with the code. I need some help please.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ahmad M Ibrahim
  • 289
  • 1
  • 3
  • 9

1 Answers1

1

The error is probably in your URL pattern -- the last keyword argument in the post_detail URL pattern should be slug, instead of 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'),
]

Also, make sure you end the URL patter with a forward slash, for consistency.

Vitor Freitas
  • 3,550
  • 1
  • 24
  • 35
  • Also, is it really wise to treat numbers that start with 0 as integers? – Marcus Lind Feb 07 '18 at 00:41
  • At first I thought the problem was the leading zero on the month, but I checked the new path `` and it converts 02 to 2 (or 0002 to 2). In the docs they use it as int as well (https://docs.djangoproject.com/en/2.0/topics/http/urls/#example). But not sure it is the best solution – Vitor Freitas Feb 07 '18 at 05:36
  • Thank you I appreciate your help – Ahmad M Ibrahim Feb 07 '18 at 07:20