【发布时间】:2014-05-11 15:01:03
【问题描述】:
我在 Django 中有一个类似这样的长 url 模式:
url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)/(?P<third_slug>[-\w]+?).html/$',
'apps.Discussion.views.pricing',
绝对不遵循 PEP8 指南,因为单行中的字符超过 80 个。我找到了两种解决方法:
第一个(使用反斜杠):
url(r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)'\
'/(?P<third_slug>[-\w]+?).html/$',
'apps.Discussion.views.pricing',
第二个——使用():
url((r'^(?i)top-dir/(?P<first_slug>[-\w]+?)/(?P<second_slug>[-\w]+?)',
r'/(?P<third_slug>[-\w]+?).html/$'),
'apps.Discussion.views.pricing'),
它们都被正则表达式打破。有没有更好的方法来解决这个问题。或者为网址编写如此长的正则表达式是一种不好的做法。
【问题讨论】: