Sovietware
Sovietware

Reputation: 1

Questions about Django urls.py

Basically, I'm writing blog software using Django/Python. Currently I have the following files

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)),
)

blog/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'),
)

My question is... Currently this means when I'm at / and I click a header link for a blog entry, I get redirected to /1/... I want it to redirect to /blog/1/ instead - is this something I need to edit in urls.py? If so, what is it I need to add?

Upvotes: 0

Views: 242

Answers (2)

Brandon Taylor
Brandon Taylor

Reputation: 34553

I'm not sure what your / url returns as far as data, but if you want all of the patterns in your blog app to start with "blog" in the URL, you can add that to the pattern in your root urls.py when you do the include:

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

Upvotes: 1

linqq
linqq

Reputation: 3067

In your main urls.py, rename urlpatterns to base_urlpatterns. Then, at the bottom, add the following:

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

Credit: lallulli, commenting on djangosnippets

Upvotes: 0

Related Questions