Sud*_*fle 15 python django pep8
我在Django中有一个长网址模式,类似于:
url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)/(?P<third_slug>[-\w]+?).html/$',
'apps.Discussion.views.pricing',
Run Code Online (Sandbox Code Playgroud)
绝对不会遵循PEP8指南,因为字符在一行中超过80.我找到了解决这个问题的两种方法:
第一个(使用反斜杠):
url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)'\
'/(?P<third_slug>[-\w]+?).html/$',
'apps.Discussion.views.pricing',
Run Code Online (Sandbox Code Playgroud)
第二个 - 使用():
url((r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)',
r'/(?P<third_slug>[-\w]+?).html/$'),
'apps.Discussion.views.pricing'),
Run Code Online (Sandbox Code Playgroud)
它们都被正则表达式打破.有没有更好的方法来解决这个问题.或者为网址编写这么长的正则表达式是不好的做法.
kha*_*ler 23
相邻的字符串是连接的,所以你可以这样做:
url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/'
r'(?P<second_slug>[-\w]+?)/'
r'(?P<third_slug>[-\w]+?).html/$',
'apps.Discussion.views.pricing',)
Run Code Online (Sandbox Code Playgroud)