gpt4 book ai didi

python - 404 错误 "No Blog entry found matching the query"Django 管理问题

转载 作者:太空宇宙 更新时间:2023-11-03 18:02:04 25 4
gpt4 key购买 nike

我在登录 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')),
)

设置.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"),
)

模型.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"

最佳答案

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

因此,访问 url 127.0.0.1:8000/admin 不会将您定向到管理部分,其正则表达式为 r'^admin/' 。您需要访问:127.0.0.1:8000/admin/(注意最后的 / )。

您收到错误“未找到与查询匹配的博客条目”的原因是 Django 认为 /admin必须并尝试满足r'^(?P<slug>\S+)$'对于 views.BlogDetail.as_view() (您应该修改谁的正则表达式以包含最后的 / 。例如 r'^(?P<slug>\S+)/' ,也许也删除 $ - 为什么要指定结尾?

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

关于python - 404 错误 "No Blog entry found matching the query"Django 管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27535864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com