我正在学习Python和Django,并且正在构建测试博客。我没有按照Django书中给出的官方教程(我发现很难理解和学习),而是按照here给出的教程。这有助于我了解管理界面以及模板部分的内容(包括一些示例HTML,我认为它不包含在官方Django书中)。
这是我的urls.py示例(这是在myproject / myproject / urls.py中):
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'myblog.views.home', name='home'),
# url(r'^myblog/', include('myblog.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('blog.urls')),
)
这是我的urls.py示例(这是在myproject / blog / urls.py中):
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^$', 'blog.views.liist'),
(r'^archive/(\d{1,2})/$', 'blog.views.liist'),
(r'^\d{4}/d{1,2}/(.*)/$', 'blog.views.detail'),
(r'^(\d{4})/(\d{1,2})/$', 'blog.views.month'),
(r'^(\d{4})/$', 'blog.views.year'),
(r'^category/$', 'blog.views.category'),
(r'^category/(.*)/$', 'blog.views.one_category'),
(r'^tag/$', 'blog.views.tag'),
(r'^tag/(.*)/$', 'blog.views.one_tag'),
)
我有什么遗漏的吗?有人可以指出解决问题的步骤,或检查什么,或哪个部分抛出错误?因为我不知道从哪里开始进行故障排除。
我需要在这里发布哪些其他信息对我的情况有帮助吗?
提前致谢。
编辑:
这就是"详细信息"函数看起来像我的views.py文件:
def detail(request,sl):
try:
post = Post.objects.filter(slug=sl)[0]
try:
previous_post = post.get_previous_by_published()
except:
previous_post = ""
try:
next_post = post.get_next_by_published()
except:
next_post = ""
except:
next_post = ""
previous_post = ""
post = ""
return render_to_response('blog/detail.html', {'post':post,
'next_post':next_post,
'previous_post':previous_post,
},)
答案 0 :(得分:2)
尝试:
(r'^(\d{4})/(\d{1,2})/([-\w]+)/$', 'blog.views.detail')
另外,请务必查看有关网址的官方文档:https://docs.djangoproject.com/en/1.4/topics/http/urls/#overview