Configurable URLs in Django
I have an app where I want the urls to have certain level of configuration, for example there's an url /jobs/programming/ and I want the 'jobs' part to be configured v开发者_开发问答ia settings.py:
JOBS_URL = 'positions'
So my urls.py looks something like this:
...
#job listing + category
url(r'^'+JOBS_URL+'/(?P<cvar_name>[-\w]+)/$',
'jobs_category',
name='job_list_category'),
...
So far so good, but all sorts of problems are to be expected if someone does something like JOBS_URL = '' or JOBS_URL = ANOTHER_URL so what's the best way to make a set of fail-proof setting variables (URLs)?
Instead of using a variable in your URL pattern, call a function to calculate the URL. That way you can do a sanity check on JOBS_URL and fall back to a reasonable default.
精彩评论