简体   繁体   English

Django 404混淆了简单页面

[英]Confused by Django 404s for simple pages

Im doing my first Django site myself after going through several tutorials and running into some errors that I can't figure out what the problem is as they are simple page requests. 我在浏览了多个教程并遇到了一些错误后,自己创建了自己的第一个Django网站,但由于它们是简单的页面请求,因此我无法弄清楚问题出在哪里。

I was trying to make a category detail view eg 127.0.0.1:8000/news and Ive followed the same setup as other pages such as the index and post detail which have worked fine but this is giving a 404. 我试图制作一个类别详细信息视图,例如127.0.0.1:8000/news,而Ive遵循与其他页面相同的设置,例如索引和帖子详细信息,它们工作正常,但是给出了404。

here are my files 这是我的文件

models.py 

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

class EntryQuerySet(models.QuerySet):
    def published(self):
        return self.filter(publish=True)

class Blog(models.Model):
    title = models.CharField(max_length = 200)
    slug = models.SlugField(max_length = 100, db_index = True)
    body = models.TextField()
    publish = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey('blog.category')

objects = EntryQuerySet.as_manager()


    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post_detail', kwargs={'slug':self.slug})


    class Meta:
        verbose_name = 'Blog entry'
        verbose_name_plural = 'Blog entries'
        ordering = ['-created']

class Category(models.Model):
    cat_title = models.CharField(max_length = 200, db_index = True)
    cat_slug = models.SlugField(max_length = 100, db_index = True)


    def __str__(self):
        return self.cat_title

    def get_absolute_url(self):
        return reverse('category_detail', kwargs={'cat_slug':self.cat_slug})

    class Meta:
        verbose_name = 'Category'
        verbose_name_plural = 'Categories'

views.py views.py

from django.views import generic
from . import models


class index_view(generic.ListView):
    queryset = models.Blog.objects.published()
    template_name = 'index.html'

class post_view(generic.DetailView):
    model = models.Blog
    template_name = 'post_view.html'

class category_view(generic.ListView):
    model = models.Category
    template_name = 'category_view.html'

class category_detail_view(generic.DetailView):
    model = models.Category
    template_name = 'category_detail_view.html'

class About_page(generic.DetailView):
    template_name = 'about.html'

app urls.py 应用urls.py

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



urlpatterns = [

    url(r'^$', views.index_view.as_view(), name='index'),
    url(r'^categories/$', views.category_view.as_view(), name='category_detail'),
    url(r'^(?P<slug>\S+)$', views.post_view.as_view(), name='post_detail'),
    url(r'^(?P<cat_slug>\S+)$', views.category_detail_view.as_view(), name='category_detail_view'),
   url(r'^about/$', views.About_page.as_view(), name='about'),

this is the category detail page 'category_detail_view.html' 这是类别详细信息页面“ category_detail_view.html”

{% extends 'base.html' %}

{% block title %} The category detail view page {% endblock %}

{% block category_detail %}
    {% for cat_title in object_list %}
        <ul>
            <li>{{ category.cat_title }}</li>

    {% endfor %}
        </ul>

{% endblock %}

and the about page 和关于页面

{% extends 'base.html' %}

<h2>This is the about page</h2>

both of these pages return this error message 这两个页面均返回此错误消息

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/about/ Raised by: blog.views.post_view No Blog entry found matching the query 找不到页面(404)请求方法:GET请求URL: http : //127.0.0.1 :8000/about/引发者:blog.views.post_view找不到与查询匹配的博客条目

I dont understand why 'blog.post_view' is being raised when neither of these templates refer to the post_view. 我不明白为什么当这两个模板均未引用post_view时引发“ blog.post_view”。

I have an index page with all published posts listed, a categories page with all categories listed and a post detail page all of which work fine and are almost exactly the same as these views and templates. 我有一个列出所有已发布帖子的索引页面,一个列出了所有类别的类别页面和一个帖子详细信息页面,所有这些都可以正常工作,并且与这些视图和模板几乎完全相同。

When Django resolves the url /about/ , it goes through your url patterns in order. Django解析网址/about/ ,它会按顺序浏览您的网址格式。 It matches the post_detail url pattern, so runs the post_view , treating about as a slug. 它匹配post_detail URL模式,所以运行post_view ,治疗about为塞。 Since you don't have any posts with the slug about , you get the 404 error. 由于您没有任何about ,因此会出现404错误。

One solution is to move the about url pattern above the post_detail pattern. 一种解决方案是将about url模式post_detail模式上方。 You should also change the category url pattern, otherwise it won't work. 您还应该更改类别网址格式,否则它将无法正常工作。 For example, you could do: 例如,您可以这样做:

url(r'^about/$', views.About_page.as_view(), name='about'),
url(r'^(?P<slug>\S+)$', views.post_view.as_view(), name='post_detail'),
url(r'^categories/(?P<cat_slug>\S+)$', views.category_detail_view.as_view(), name='category_detail_view'),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM