Django / admin / Page not found(404)

时间:2016-10-01 10:40:29

标签: python django

我有以下错误:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/admin/
Raised by:  nw.views.post_detail

但我可以在示例中输入http://127.0.0.1:8000/admin/nw/

如果我删除所有post_detail函数/部分代码,那么我可以输入http://127.0.0.1:8000/admin/并且它可以工作,所以这个post_detail是错误的。

nw.views.post_detail:

def post_detail(request, slug=None):
    instance = get_object_or_404(Post, slug=slug)

    context = {
        "instance": instance,
    }
    return render(request, "post_detail.html", context)

urls.py:

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

urlpatterns = [
    url(r'^$', post_list, name='list'),
    url(r'^create/$', post_create),
    url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
    url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
    url(r'^(?P<slug>[\w-]+)/delete/$', post_delete),
]

post_detail.html:

{% extends 'base.html' %}

{% block head_title %}{{ instance.title }} | {{ block.super }}{% endblock head_title %}

{% block content %}
    <div class="instance">
        <h1>{{ instance.title }}</h1>
        <div class="date">{{ instance.published }}</div>
        <p>{{ instance.text|linebreaksbr }}</p>

        <a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}">
        Facebook
        </a>

        <a href="https://twitter.com/home?status={{ share_string }}%20{{ request.build_absolute_uri }}">
        Twitter
        </a>

        <a href='https://plus.google.com/share?url={{ request.build_absolute_uri }}'>google</a>


        <a href="https://www.linkedin.com/shareArticle?mini=true&url={{ request.build_absolute_uri }}&title={{ instance.title }}&summary={{ share_string }}&source={{ request.build_absolute_uri }}">
        Linkedin
        </a>

        <a href="http://www.reddit.com/submit?url={{ request.build_absolute_uri }}&title={{ share_string }}.">Reddit</a>

    </div>

{% endblock %}

models.py:

class Post(models.Model):
    author = models.ForeignKey('auth.User', default=1)
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    text = models.TextField()
    published = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})

    class Meta:
        ordering = ["-published"]


def create_slug(instance, new_slug=None):
    slug = slugify(instance.title)
    if new_slug is not None:
        slug = new_slug
    qs = Post.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = create_slug(instance)


pre_save.connect(pre_save_post_receiver, sender=Post)

2 个答案:

答案 0 :(得分:2)

admin url 应该是第一个:

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

否则,它会被您的帖子应用urlpatterns中的其他匹配正则结构拦截,即:

url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),

根据经验,始终将管理网址保留在项目的顶部urlpatterns

答案 1 :(得分:1)

在您的情况下,此路由正在运行url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),,此函数名为instance = get_object_or_404(Post, slug=slug)

你可以像这样替换路线

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

路线的顺序很重要