0

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?

1
  • 1
    What problems did it cause? Using include('') is usually pretty straightforward. Could you describe the errors it gave, or how the output differed from what you were expecting? Commented Jul 15, 2009 at 2:15

3 Answers 3

3

I'm no django expert, but wouldn't the 'view1' item match all of the other entries below it since it doesn't have a '$' at the end? So the other views wouldn't have a chance to get matched.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 put the catch-all at the end or just include $ for every url.
2

I don't think you can do what you are trying to do with this line:

(ur'^(?P<url_key>[-a-zA-Z0-9]+)/', include('myproject.myapp.urls2'))

What view is the url_key parameter going to be passed to?

I'm not sure why you want to refactor the urlpatterns to begin with, but maybe this would be better?:

from django.conf.urls.defaults import *

URL_KEY = ur'^(?P<url_key>[-a-zA-Z0-9]+)'

urlpatterns = patterns('myproject.myapp.views',
    (ur'^$', 'index'),
    (ur'^browse/$', 'browse'),
    (ur'^request/new/$', 'new_request'),
    (URL_KEY+ur'/$', 'view1'),
    (URL_KEY+ur'/asdf$', 'view2'),
    (URL_KEY+ur'/qwer$', 'view3'),
    ...etc
)

1 Comment

Well, I've fixed my test problems and adopted my original solution still, but this is a good suggestion. So I choose this as answer. :)
0

Perhaps you could simplify the expressions in myproject.myapp.urls, and instead pass the information along as a parameter to a function in myproject.myapp.views?

I'm not sure what was going wrong in your tests, but generally speaking you'll be able to do more in myproject.myapp.views since you don't have to base it all on regex logic.

That function in myproject.myapp.views would be a switchboard that calls view1, view2, etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.