错误:使用mysite.urls中定义的URLconf,Django尝试了这些URL模式

时间:2017-10-11 17:19:01

标签: django

我正在做教程" Blog"来自" Django的例子"我得到了错误。 http://127.0.0.1:8000/admin/运行正常。我做错了什么?

错误: 找不到页(404) 请求方法:GET 请求网址:http://127.0.0.1:8000/post/ 使用mysite.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式: ^管理员/ 当前网址post /与这些网址中的任何一个都不匹配。

mysite的/博客/ 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的/博客/ 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的/博客/ 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的/博客/模型

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 个答案:

答案 0 :(得分:3)

您将博客网址包含在:

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

因此,您应该转到http://127.0.0.1:8000/blog/,而不是http://127.0.0.1:8000/post/才能看到post_list视图。

请注意,您应该在博客网址中使用列表而不是集合。用方括号替换花括号。

urlpatterns = [
    ...
]

答案 1 :(得分:0)

错误表示您未在/post/中定义urls.py个网址。

您应该将网址更改为:

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

或者,访问/blog/的帖子列表。

我认为第一个版本是更好的选择。

希望它有所帮助!