我一直在努力完成Django webmonkey教程,目前我在第4课中遇到http://www.webmonkey.com/2010/02/Use_Templates_in_Django/。
我的问题在于博客详细信息模板,每当我点击我的blog / list.html页面上的链接来查看条目的详细信息时,我找不到页面(404)。
这正是我看到的错误:
Page not found (404)
Request Method: GET
Request URL:http://127.0.0.1:8000/2010/dec/17/welcome-my-blog/
Using the URLconf defined in djangoblog.urls, Django tried these URL patterns, in this order:
^admin/(.*)
^blog/
^tags/(?P<slug>[a-zA-Z0-9_.-]+)/$
The current URL, 2010/dec/17/welcome-my-blog/, didn't match any of these.
这些是我的url.py文件以及我的models.py,我没有发布我的管理员,标记视图或设置,但我可以,如果它会有帮助。
djangoblog \ urls.py
# This also imports the include function
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^blog/', include('djangoblog.blog.urls')),
(r'^tags/(?P<slug>[a-zA-Z0-9_.-]+)/$', 'djangoblog.tag_views.tag_detail'),
)
djangoblog \博客\ urls.py
from django.conf.urls.defaults import *
from djangoblog.blog.models import Entry
from tagging.views import tagged_object_list
info_dict = {
'queryset': Entry.objects.filter(status=1),
'date_field': 'pub_date',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(?P<slug>[-w]+)/$',
'object_detail', dict(info_dict, slug_field='slug',template_name='blog/detail.html')),
(r'^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(?P<slug>[-w]+)/$',
'object_detail', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/$',
'archive_day',dict(info_dict,template_name='blog/list.html')),
(r'^(?P<year>d{4})/(?P<month>[a-z]{3})/$','archive_month', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>d{4})/$','archive_year', dict(info_dict, template_name='blog/list.html')),
(r'^$','archive_index', dict(info_dict, template_name='blog/list.html')),
)
djangoblog \博客\ models.py
from django.db import models
from django.contrib.syndication.feeds import Feed
from django.contrib.sitemaps import Sitemap
import markdown
from tagging.fields import TagField
from tagging.models import Tag
class Entry(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(
unique_for_date='pub_date',
help_text='Automatically built from the title.'
)
body_html = models.TextField(blank=True)
body_markdown = models.TextField()
pub_date = models.DateTimeField('Date published')
tags = TagField()
enable_comments = models.BooleanField(default=True)
PUB_STATUS = (
(0, 'Draft'),
(1, 'Published'),
)
status = models.IntegerField(choices=PUB_STATUS, default=0)
class Meta:
ordering = ('-pub_date',)
get_latest_by = 'pub_date'
verbose_name_plural = 'entries'
def __unicode__(self):
return u'%s' %(self.title)
def get_absolute_url(self):
return "/%s/%s/" %(self.pub_date.strftime("%Y/%b/%d").lower(), self.slug)
def save(self):
self.body_html = markdown.markdown(self.body_markdown, safe_mode = False)
super(Entry, self).save()
def get_previous_published(self):
return self.get_previous_by_pub_date(status__exact=1)
def get_next_published(self):
return self.get_next_by_pub_date(status__exact=1)
def get_tags(self):
return Tag.objects.get_for_object(self)
如果有任何其他文件可以帮助我提供它们。我的文件结构如下:
文件结构
C:\Workspaces\python\djangoblog
urls.py
tag_views.py
settings.py
manage.py
djangoblog.db
admin.py
__init__.py
templates
blog
detail.html
list.html
tags
detail.html (empty)
list.html (empty)
base.html
tagging
markdown
blog
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
编辑:(评论Josh的回答)
404 at:blog / dec / 17 / welcome-my-blog /:
^admin/(.*)
^blog/ (?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(? P<slug>[-w]+)/$
^blog/ ^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(? P<slug>[-w]+)/$
^blog/ ^(?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/$
^blog/ ^(?P<year>d{4})/(?P<month>[a-z]{3})/$
^blog/ ^(?P<year>d{4})/$
^blog/
^$ ^tags/(?P<slug>[a-zA-Z0-9_.-]+)/$
The current URL, blog/2010/dec/17/welcome-my-blog/, didn't match any of these.
答案 0 :(得分:3)
问题是您的网址模式都没有与浏览器中的网址匹配。
http://127.0.0.1:8000/2010/dec/17/welcome-my-blog/
您尝试访问的网址。我认为这是一个博客条目,可以通过URL blog/
访问。
您的根网址格式如下:
urlpatterns = patterns('',
(r'^admin/(.*)', admin.site.root),
(r'^blog/', include('djangoblog.blog.urls')),
(r'^tags/(?P[a-zA-Z0-9_.-]+)/$', 'djangoblog.tag_views.tag_detail'),
)
这意味着它们都不会与您在浏览器中的网址匹配,因为它不是以admin
或blog
或tags
开头的。
以下网址可能会解决您的问题:
http://127.0.0.1:8000/blog/2010/dec/17/welcome-my-blog/
注意IP:端口之后的/blog/
。这将匹配根URL conf中的blog/
条目。然后由djangoblog.blog.urls
文件来处理模式的其余部分,它看起来就是这样。
(r'(?Pd{4})/(?P[a-z]{3})/(?Pw{1,2})/(?P[-w]+)/$', 'object_detail', dict(info_dict, slug_field='slug',template_name='blog/detail.html'))
以上模式寻找:
- 4个数字(2010)
- 然后a /
- 然后3个字母(dec)
- 然后a /
- 然后是一个带有1或2个字符的单词(17)
- 然后a /
- 然后一个字(welcome-my-blog)
通过在IP:端口之后添加/blog/
,您应该没问题。
您的更新:
^blog/ (?P<year>d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(? P<slug>[-w]+)/$
以上网址不正确。您需要在^之前添加其他URL。
答案 1 :(得分:1)
对于那些像我一样错过它的人,主要的变化是将此更改为P<year>d{4}
到此P<year>\d{4}
。这是添加反斜杠。您可能还希望查看行尾的/$
。这些将强制您的网址以“/".
答案 2 :(得分:0)
想出来,Josh对于缺少/ blog的URL模式是正确的。我更新了我的列表模板:
<p><a href="/blog{{object.get_absolute_url}}">read more</a></p>
解决了/ blog错误,之后我需要修改我的djangoblog \ blog \ urls.py,如下所示:
<强> djangoblog \博客\ urls.py 强>
urlpatterns = patterns('django.views.generic.date_based',
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/(?P<slug>[-\d\w]+)/$', 'object_detail', dict(info_dict, slug_field='slug',template_name='blog/detail.html')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/(?P<slug>[-\d\w]+)/$', 'object_detail', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/$','archive_day',dict(info_dict,template_name='blog/list.html')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','archive_month', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>\d{4})/$','archive_year', dict(info_dict, template_name='blog/list.html')),
(r'^$','archive_index', dict(info_dict, template_name='blog/list.html')),
)
一旦我完成了这两项更改,我的所有网址都已修复,我无法理解我在此网站上找到的urls.py修补程序:
http://ubuntuforums.org/showthread.php?p=9650959
希望通过本教程可以帮助其他人。