Created
October 16, 2011 01:57
-
-
Save fish2000/1290418 to your computer and use it in GitHub Desktop.
Creating URL namespaces in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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