我想知道如果使用多个嵌套模式组重构我的应用程序的urlconf,将为Django寻找请求匹配模式提供性能提升。我不确定如何测试性能,但我已经尝试过,新版本起作用了。
我知道如果性能提升,它可能会微不足道。我只是开始这样做,看看模式是否更容易看作树;我更担心搞砸事情而不是提高速度。
以下是主用urlconf在/appname/
包含的app的urlconf。
旧urlconf:
from django.contrib.auth.decorators import login_required
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
from appname import views
urlpatterns = patterns('',
# Actions on sets of reports
url(r'^reports/page(?P<page>[0-9]+)/$', login_required(views.appnameListView.as_view())),
url(r'^reports/bill/', views.bill),
# Actions on individual reports
url(r'^report/new/', views.new),
url(r'^report/(?P<reportNumber>[^/]+)/$', views.detail),
url(r'^report/(?P<reportNumber>[^/]+)/delete/$', views.delete),
url(r'^report/(?P<reportNumber>[^/]+)/edit/$', views.edit),
url(r'^report/(?P<reportNumber>[^/]+)/print/$', views.detail_print),
# url(r'^report/(?P<reportNumber>[^/]+)/pdf/$', views.detail_pdf),
# Configure app settings
url(r'^config/$', views.configure),
url(r'^config/reporttype/(?P<id>[^/]+)/$', views.configure_ReportType),
url(r'^config/reporttype/(?P<id>[^/]+)/delete/$', views.configure_ReportType_delete),
url(r'^config/avgmethod/(?P<name>[^/]+)/$', views.configure_AvgMethod),
url(r'^config/avgmethod/(?P<name>[^/]+)/delete/$', views.configure_AvgMethod_delete),
url(r'^config/datatransformer/(?P<name>[^/]+)/$', views.configure_DataTransformer),
url(r'^config/datatransformer/(?P<name>[^/]+)/delete/$', views.configure_DataTransformer_delete),
# Catch all; #TODO: 404
url(r'^', redirect_to, {'url': '/appname/reports/page1/'}),
)
新的urlconf:
from django.contrib.auth.decorators import login_required
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
from appname import views
urlpatterns = patterns('',
# Actions on sets of reports
url(r'^reports/', include(patterns('',
url(r'^page(?P<page>[0-9]+)/$', login_required(views.appnameListView.as_view())),
url(r'^bill/', views.bill),
))),
# Actions on individual reports
url(r'^report/', include(patterns('',
url(r'^new/', views.new),
url(r'^(?P<reportNumber>[^/]+)/$', views.detail),
url(r'^(?P<reportNumber>[^/]+)/delete/$', views.delete),
url(r'^(?P<reportNumber>[^/]+)/edit/$', views.edit),
url(r'^(?P<reportNumber>[^/]+)/print/$', views.detail_print),
# url(r'^(?P<reportNumber>[^/]+)/pdf/$', views.detail_pdf),
))),
# Configure app settings
url(r'^config/', include(patterns('',
url(r'^$', views.configure),
url(r'^reporttype/(?P<id>[^/]+)/$', views.configure_ReportType),
url(r'^reporttype/(?P<id>[^/]+)/delete/$', views.configure_ReportType_delete),
url(r'^avgmethod/(?P<name>[^/]+)/$', views.configure_AvgMethod),
url(r'^avgmethod/(?P<name>[^/]+)/delete/$', views.configure_AvgMethod_delete),
url(r'^datatransformer/(?P<name>[^/]+)/$', views.configure_DataTransformer),
url(r'^datatransformer/(?P<name>[^/]+)/delete/$', views.configure_DataTransformer_delete),
))),
# Catch all; #TODO: 404
url(r'^', redirect_to, {'url': '/appname/reports/page1/'}),
)
我可以更进一步(使用正则表达式检查的5个变量中的4个被定义至少两次),但我担心失去可读性的原始好处。
答案 0 :(得分:2)
如果您不想重新考虑并遵循DRY原则(在开发django代码时有时会合并很多),您应该对您的网址进行分组。因此,您将减少重复代码的数量并生成更易于维护的代码。举例来说,事实上,您可能希望将报告/网址更改为其他内容,然后您必须为每个网址执行此操作!