404错误"找不到与查询匹配的博客条目" Django管理员问题

时间:2014-12-17 22:15:55

标签: python django

我在登录Django管理员时遇到问题,这可能是缺少路线的问题。我看过类似的Stack Overflow问题,但那个人似乎有一个different issue但没有答案。

错误消息

我收到的错误消息很短,基本上说"找不到与查询匹配的博客条目。"

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin
No Blog entry found matching the query

这两个与我的项目相关

urls.py

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

urlpatterns = patterns(
    '',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^markdown/', include("django_markdown.urls")),
    url(r'^', include('blog.urls')),
)

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'blog',
    'django_markdown',
)

这三个与我的应用相关

urls.py

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

urlpatterns = patterns(
    '',
    url(r'^$', views.BlogIndex.as_view(), name="list"),
    url(r'^(?P<slug>\S+)$', views.BlogDetail.as_view(), name="detailed"),
)

Models.py

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class FullArticleQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)

class FullArticle(models.Model):
    title = models.CharField(max_length=150)
    author = models.CharField(max_length=150)
    slug = models.SlugField(max_length=200, unique=True)
    pubDate = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    category = models.CharField(max_length=150)
    heroImage = models.CharField(max_length=250, blank=True)
    relatedImage =  models.CharField(max_length=250, blank=True)
    body =  models.TextField()
    publish = models.BooleanField(default=True)
    gameRank = models.CharField(max_length=150, blank=True, null=True)

    objects = FullArticleQuerySet.as_manager()

    def __str__(self):
        return self.title

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

    class Meta:
        verbose_name = "Blog entry"
        verbose_name_plural = "Blog Entries"
        ordering = ["-pubDate"]

views.py

from django.views import generic
from . import models 

# Create your views here.
class BlogIndex(generic.ListView):
    queryset = models.FullArticle.objects.published()
    template_name = "list.html" 
    randomArticle = models.FullArticle.objects.order_by('?').first()

class BlogDetail(generic.DetailView):
    model = models.FullArticle
    template_name = "detailed.html"

2 个答案:

答案 0 :(得分:1)

这是一系列问题。您已输入的网址http://127.0.0.1/admin 与正则表达式r'^admin/'匹配。根据错误消息,您的BlogDetail视图似乎已被调用,因此URL路由必须跳过管理网址。

将斜杠添加到您请求的网址,它应该有效。

我还没有对此进行过测试,这只是我的头脑。

答案 1 :(得分:0)

Django的ulrpattern正则表达式是贪婪的,即它们必须完全匹配。

因此,访问网址127.0.0.1:8000/admin不会将您引导至管理部分,其正则表达式为r'^admin/'。您需要访问:127.0.0.1:8000 / admin /(注意最终的/)。

您收到错误的原因&#34;找不到与查询匹配的博客条目&#34;是因为Django认为/admin必须是r'^(?P<slug>\S+)$' views.BlogDetail.as_view() /(你应该修改谁的正则表达式以包含最终r'^(?P<slug>\S+)/'。例如{{1}也许放弃$ - 为什么要指定一个结尾?

另外,为什么使用\S\w