I am doing something like this in myproject.myapp.urls
:
from django.conf.urls.defaults import *
urlpatterns = patterns('myproject.myapp.views',
(ur'^$', 'index'),
(ur'^browse/$', 'browse'),
(ur'^request/new/$', 'new_request'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/$', 'view1'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/asdf$', 'view2'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/qwer$', 'view3'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/zxcv$', 'view4'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/tyui$', 'view5'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/ghjk$', 'view6'),
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/bnm/more-looong-url/$', 'view7'),
...
)
I've tried to refactor above rules and define them in another file urls2.py
like this:
(ur'^(?P<url_key>[-a-zA-Z0-9]+)/', include('myproject.myapp.urls2')),
but it seems to cause problems with unit tests including urlresolvers.
Is there better way to "refactor" the common part of regular expression (<url_key>
) here?