关于Django urls.py的问题

时间:2011-11-19 21:02:15

标签: python django django-urls

基本上,我正在使用Django / Python编写博客软件。目前我有以下文件

urls.py:

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Example:
    # (r'^sovietware/', include('sovietware.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    (r'^', include('sovietware.blog.urls')),
    (r'^comments/', include('django.contrib.comments.urls')),
    (r'^admin/', include(admin.site.urls)),
)

博客/ urls.py:

from django.conf.urls.defaults import *
from models import Post

queryset    = {'queryset': Post.objects.order_by('-pub_date')}
urlpatterns = patterns('django.views.generic.list_detail',
    url('^$', 'object_list', queryset, name='posts'),
    url('^(?P<object_id>\d+)/$', 'object_detail', queryset, name='post'),
)

我的问题是......目前这意味着当我在/并且我点击博客条目的标题链接时,我被重定向到/ 1 / ...我希望它重定向到/ blog / 1 /相反 - 这是我需要在urls.py中编辑的内容吗?如果是这样,我需要添加什么?

2 个答案:

答案 0 :(得分:1)

我不确定您的/ url返回的数据是什么,但如果您希望博客应用中的所有模式都以URL中的“blog”开头,则可以将其添加到根目录中的模式中urls.py当你做include:

(r'^blog/', include('sovietware.blog.urls')),

答案 1 :(得分:0)

在主urls.py中,将urlpatterns重命名为base_urlpatterns。然后,在底部添加以下内容:

urlpatterns = patterns('',
    '^', include(base_urlpatterns), # iff you wish to maintain the un-prefixed URLs too
    '^your_prefix/', include(base_urlpatterns),
)

信用:lallulli, commenting on djangosnippets