Designer023
Designer023

Reputation: 2002

Django urls.py problem passing variables

I have an app called portfolio and I am trying to map out the pages so that i end up with a fixed area that always exists called 'gallery'. I have set this up and it's working fine, but the gallery items are mapped to page_type areas, such as 'images', 'videos' etc so I wanted my root urls.py to detect this and then send t the correct view but I cant figure out how to do it

root urls.py

urlpatterns = patterns('',

 (r'^(?P<page_type>[a-zA-Z0-9-]+)/$', include('portfolio.urls')),
 (r'^gallery/', include('portfolio.urls')),

 (r'^admin/(.*)', admin.site.root)
)

portfolio urls.py

urlpatterns = patterns('portfolio.views',
 #(r'^(?P<gallery_type>\d+)/$', 'index'),
 (r'^page/(?P<page_number>[0-9]+)/$', 'index'),
 (r'^(?P<page_category>[a-zA-Z0-9-]+)/$', 'category_index'),
 (r'^(?P<page_category>[a-zA-Z0-9-]+)/page/(?P<page_number>[0-9]+)/$', 'category_index'),
 (r'^$', 'index'),
)

Is it even possible? And how? I can't find any info on passing the matching expressions etc.

PLease help. Thanks :)

A friend has pointed out that I could go directly to the views rather than go via the apps urls.py by doing something like this [code] (r'^(?P[a-zA-Z0-9-]+)/(?P[a-zA-Z0-9-]+)/$', 'portfolio.views.detail'), [/code]

and then accessing it using: [code] def detail(request, page_type, page_name): ... [/code]

Upvotes: 0

Views: 607

Answers (1)

Bernhard Vallant
Bernhard Vallant

Reputation: 50786

You have to move (r'^gallery/', include('portfolio.urls')), BEFORE (r'^(?P<page_type>[a-zA-Z0-9-]+)/$', include('portfolio.urls')), because the page type regex will also match 'gallery/' and the patterns are apllied in the order as they are defined!

Upvotes: 5

Related Questions