Skip to content

Instantly share code, notes, and snippets.

@cooncesean
Created December 11, 2013 06:17
Show Gist options
  • Save cooncesean/7905831 to your computer and use it in GitHub Desktop.
Save cooncesean/7905831 to your computer and use it in GitHub Desktop.
First hack at "dynamic" urlpatterns.
# gears/urls.py =>
from corpsite.kr.urls import urlpatterns as corp_kr_urlpatterns
# corpsite/base_urls.py
BASE_PATTERNS = [
{
'regex': r'^guide/(?P<guide_id>[0-9]+)/delete/$',
'view': 'path.to.some.view', # missing
'name': 'delete-guide' # semi-known
},
]
def build_site_urls(views_module, prefix):
"""
Dynamically build and return a valid set of urlpatterns based
on the apps `prefix` and core `views` module.
"""
urlpatterns = ()
for pattern_dict in BASE_PATTERNS:
urlpatterns += patterns('',
url(pattern_dict['regex'], views_module.some_view., name='%s-%s' % (prefix, pattern_dict['name'])),
)
return urlpatterns
# corpsite/kr/urls.py =>
from corpsite.base_urls import BASE_PATTERNS, build_site_urls
from corpsite.kr import views as korea_views
urlpatterns = build_site_urls(korea_views, 'kr')
urlpatterns += (
# ...any other patterns that are not in base ...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment