3
votes

I'm doing tutorial "Blog" from "Django by example" and i got error. http://127.0.0.1:8000/admin/ works fine. What i'm doing wrong?

Error: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/post/ Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^admin/ The current URL, post/, didn't match any of these.

mysite/blog/urls.py

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

urlpatterns = {
    # post views
    url(r'^$', views.post_list, 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'),
}

mysite/blog/views.py

from django.shortcuts import render, get_object_or_404
from .models import Post


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__mounth=month,
                             publish__day=day)
    return render(request,
                  'blog/post/detail.html',
                  {'post': post})

mysite/blog/admin.py

from django.contrib import admin
from .models import Post


class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'author', 'publish', 'status')
    list_filter = ('status', 'created', 'publish', 'author')
    search_fields = ('title', 'body')
    prepopulated_fields = {'slug': ('title',)}
    raw_id_fields = ('author',)
    date_hierarchy = 'publish'
    ordering = ['status', 'publish']

admin.site.register(Post, PostAdmin)

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

mysite/blog/models

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse


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

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

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

        def __str__(self):
            return self.title
2

2 Answers

3
votes

You are including the blog urls with:

url(r'^blog/', include('blog.urls',
                       namespace='blog',
                       app_name='blog')),

Therefore you should go to http://127.0.0.1:8000/blog/, not http://127.0.0.1:8000/post/ to see your post_list view.

Note that you should use a list and not a set in your blog URLs. Replace the curly braces with square brackets.

urlpatterns = [
    ...
]
0
votes

The error means that you don't have /post/ url defined in your urls.py.

You should either change your urls to this:

urlpatterns = {
    # post views
    url(r'^post/$', views.post_list, name='post_list'),
    ...
}

Or, access the list of posts at /blog/.

I think the first version would be a better choice.

Hope it helps!