Skip to content

Instantly share code, notes, and snippets.

@fish2000
Created October 16, 2011 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fish2000/1290418 to your computer and use it in GitHub Desktop.
Save fish2000/1290418 to your computer and use it in GitHub Desktop.
Creating URL namespaces in Django
from django.conf.urls import patterns, include, url
# you can only define a namespace for urls when calling include() -- hence this rigamarole:
app_patterns = patterns('',
url(r'^(?P<pk>[\w\-]+)/$', 'yourapp.views.your_view_function',
name="your-view"),
)
urlpatterns = patterns('',
url(r'^view-function/', include(app_patterns,
namespace='yournamespace', app_name='yourapp')),
)
"""
You can now use the namespace when you refer to the view, e.g. a call
to `reverse()`:
# yourapp/models.py
from django.core.urlresolvers import reverse
# ...
class MyModel(models.Model):
def get_absolute_url(self):
return reverse('signalqueue:exception-log-entry', kwargs=dict(pk=self.pk))
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment