Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing Django URLs With Extra Options

Tags:

python

django

Suppose I have a URLconf like below, and 'foo' and 'bar' are valid values for page_slug.

urlpatterns = patterns('',
    (r'^page/(?P<page_slug>.*)/', 'myapp.views.someview'),
)

Then, I could reconstruct the URLs using the below, right?

>>> from django.core.urlresolvers import reverse
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'foo'})
'/page/foo/'
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'bar'})
'/page/bar/'

But what if I change my URLconf to this?

urlpatterns = patterns('',
    (r'^foo-direct/', 'myapp.views.someview', {'page_slug': 'foo'}),
    (r'^my-bar-page/', 'myapp.views.someview', {'page_slug': 'bar'}),
)

I expected this result:

>>> from django.core.urlresolvers import reverse
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'foo'})
'/foo-direct/'
>>> reverse('myapp.views.someview', kwargs={'page_slug': 'bar'})
'/my-bar-page/'

However, this throws a NoReverseMatch exception. I suspect I'm trying to do something impossible. Any suggestions on a saner way to accomplish what I want?

Named URLs aren't an option, since I don't want other apps that link to these to need to know about the specifics of the URL structure (encapsulation and all that).

like image 644
Justin Voss Avatar asked Mar 18 '09 19:03

Justin Voss


People also ask

What does the Django URLs reverse () function do?

reverse_lazy()providing a reversed URL as the url attribute of a generic class-based view. providing a reversed URL to a decorator (such as the login_url argument for the django.

How do I pass URL parameters in Django?

Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.

What happens if you skip trailing slash in Django?

The local Django webserver will automatically restart to reflect the changes. Try again to refresh the web page for the User section of the admin without the trailing slash: 127.0. 0.1:8000/admin/auth/user .

What happens when a URL is matched Django?

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info . Once one of the URL patterns matches, Django imports and calls the given view, which is a Python function (or a class-based view).


2 Answers

You should try naming your urlconfs. Example:

urlpatterns = patterns('',
    url(r'^foo-direct/', 'myapp.views.someview', {'page_slug': 'foo'}, name='foo-direct'),
    url(r'^my-bar-page/', 'myapp.views.someview', {'page_slug': 'bar'}, name='bar-page'),
)

Then just edit your reverses and you should get it working.

>>> from django.core.urlresolvers import reverse
>>> reverse('foo-direct', kwargs={'page_slug': 'foo'})
'/foo-direct/'
>>> reverse('bar-page', kwargs={'page_slug': 'bar'})
'/my-bar-page/'

More info at: Django Docs

like image 180
toqueteos Avatar answered Sep 25 '22 01:09

toqueteos


Here's what we do.

urls.py has patterns like this

url(r'^(?P< datarealm >.*?)/json/someClass/(?P<object_id>.*?)/$', 'json_someClass_resource', ),

views.py as reverse calls like this

    object = SomeModel.objects.get(...)
    url= reverse('json_someClass_resource', kwargs={'object_id':object.id,'datarealm':object.datarealm.name})
like image 32
S.Lott Avatar answered Sep 25 '22 01:09

S.Lott