29👍
✅
Adjacent strings are concatenated, so you can do something like this:
url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/'
r'(?P<second_slug>[-\w]+?)/'
r'(?P<third_slug>[-\w]+?).html/$',
'apps.Discussion.views.pricing',)
-2👍
PEP8 has no regex formatting tips. But try these:
- use re.compile and have these benefits
- quicker to match/search on them
- reference them under a (short) name!
- write the regex multiline with (white)spaces
- use re.VERBOSE to ignore whitespace in the regex string
- use flags instead of “magic groups” (
(?i) → re.IGNORECASE
)
slugs = re.compile(r'''
^
top-dir/
(?P<first_slug>[-\w]+?)/
(?P<second_slug>[-\w]+?)/
(?P<third_slug>[-\w]+?).html/
$
''', re.VERBOSE|re.IGNORECASE)
url(slugs, 'apps.Discussion.views.pricing', ...)
- Assign Value of Named URL to a Variable in Django Templates
- Is exposing a session's CSRF-protection token safe?
- Tastypie Negation Filter
- Creation of dynamic model fields in django
- How to specify uniqueness for a tuple of field in a Django model
Source:stackexchange.com