Rahadian Wiratama
Rahadian Wiratama

Reputation: 344

Django 404 Page Not Found blog.views.post_detail

I'm new with django and I use mysql. I followed the tutorial to make blog with django. I made list and detail blog, but when I clicked the detail post of blog, the error comes Page Not Found (404) Raised by: blog.views.post_detail. These are my site urls.py, blog urls.py, models.py and views.py.

Site urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/', include('blog.urls',
                            namespace='blog',
                            app_name='blog')),
] 

blog urls.py:

from django.conf.urls import url,include
from . import views

urlpatterns = [
    url(r'^$', views.PostListView.as_view(),name='post_list'),
    url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\
        r'(?P<post>[-\w]+)/$',
        views.post_detail,
        name='post_detail'),
]

models.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager,self).get_queryset()\
            .filter(status='published')
class Post(models.Model):
    STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),)
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,unique_for_date='publish')
    author = models.ForeignKey(User,related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')
    objects = models.Manager()
    published = PublishedManager()

    class Meta:
        ordering=('-publish',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail',
        args=[self.publish.year,
             self.publish.strftime('%m'),
             self.publish.strftime('%d'),
             self.slug])

views.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render,get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
def post_list(request):
    object_list = Post.published.all()
    paginator = Paginator(object_list,3)
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    return render(request,'blog/post/list.html',{'page':page,'posts':posts})

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

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

Upvotes: 1

Views: 1044

Answers (1)

Newell
Newell

Reputation: 177

I think maybe you should use "pk" as an argument instead of complicated urls that you have got

def post_detail(request, pk):
    post = get_object_or_404(Post,pk=pk)
    return render (request,
                'blog/post/detail.html',
                {'post':post})

urlpatterns = [
    url(r'^$', views.PostListView.as_view(),name='post_list'),
    url(r'^(?P<pk>[0-9]+)/$',
        views.post_detail,
        name='post_detail'),
        ]

Just don't complicate things nobody loves this

And if you still want it to be your way, try to make it super simple from the start and then add your stuff to see what went really wrong.

From the beginning use id, then try to add a year, then a month and then other stuff. You will find what's wrong

Upvotes: 1

Related Questions