gpt4 book ai didi

python - 对相似的 URL 模式进行分组

转载 作者:行者123 更新时间:2023-12-01 06:00:52 27 4
gpt4 key购买 nike

有没有办法简化以<product>开头的模式而不将它们放在单独的文件中?

urlpatterns = patterns('',
# Basic product functions
url(r'^create/$', 'product.views.create'),
url(r'^save/$', 'product.views.save'),
url(r'^save/(?P<product>[a-z0-9\-]+)/$', 'product.views.save'),
url(r'^edit/(?P<product>[a-z0-9\-]+)/$', 'product.views.edit'),
url(r'^publish/(?P<product>[a-z0-9\-]+)/$', 'product.views.publish'),
url(r'^upload_image/(?P<product>[a-z0-9\-]+)/$', 'product.views.upload_image'),

# Blog
url(r'^(?P<product>[a-z0-9\-]+)/blog/(?P<blog_id>[0-9]+)/view/(?P<content_id>[0-9]+)/$', 'blog.views.view'),
url(r'^(?P<product>[a-z0-9\-]+)/blog/(?P<blog_id>[0-9]+)/$', 'blog.views.main'),
url(r'^(?P<product>[a-z0-9\-]+)/blog/(?P<blog_id>[0-9]+)/(?P<page>[0-9]+)/$', 'blog.views.main'), #pagination for main

# Categories
url(r'^(?P<product>[a-z0-9\-]+)/category/(?P<category_id>[0-9]+)/$', 'category.views.category'),
url(r'^(?P<product>[a-z0-9\-]+)/category/(?P<blog_id>[0-9]+)/category/(?P<category_id>[0-9]+)/(?P<page>[0-9]+)/$', 'category.views.category'), #pagination for main

# Content
url(r'^(?P<product>[a-z0-9\-]+)/content/(?P<blog_id>[0-9]+)/edit/$', 'content.views.edit'),
url(r'^(?P<product>[a-z0-9\-]+)/content/(?P<blog_id>[0-9]+)/edit/(?P<content_id>[0-9]+)/$', 'content.views.edit'),
)

只是寻找任何可能简化这里内容的方法。

是否有某种包装可以将所有 ^<product> 包裹起来模式来预处理它?<​​/p>

最佳答案

您好,在 django 文档中您可以找到:

from django.conf.urls import patterns, url, include

extra_patterns = patterns('',
url(r'^reports/(?P<id>\d+)/$', 'credit.views.report', name='credit-reports'),
url(r'^charge/$', 'credit.views.charge', name='credit-charge'),
)

urlpatterns = patterns('',
url(r'^$', 'apps.main.views.homepage', name='site-homepage'),
(r'^help/', include('apps.help.urls')),
(r'^credit/', include(extra_patterns)),
)

所以你可以这样做:

extra_patterns = patterns('',
url(r'blog/(?P<blog_id>[0-9]+)/view/(?P<content_id>[0-9]+)/$', 'blog.views.view'),
url(r'blog/(?P<blog_id>[0-9]+)/$', 'blog.views.main'),
url(r'blog/(?P<blog_id>[0-9]+)/(?P<page>[0-9]+)/$', 'blog.views.main'), #pagination for main

# Categories
url(r'category/(?P<category_id>[0-9]+)/$', 'category.views.category'),
url(r'category/(?P<blog_id>[0-9]+)/category/(?P<category_id>[0-9]+)/(?P<page>[0-9]+)/$', 'category.views.category'), #pagination for main

# Content
url(r'^content/(?P<blog_id>[0-9]+)/edit/$', 'content.views.edit'),
url(r'^content/(?P<blog_id>[0-9]+)/edit/(?P<content_id>[0-9]+)/$', 'content.views.edit'),
)


urlpatterns = patterns('',
# Basic product functions
url(r'^create/$', 'product.views.create'),
url(r'^save/$', 'product.views.save'),
url(r'^save/(?P<product>[a-z0-9\-]+)/$', 'product.views.save'),
url(r'^edit/(?P<product>[a-z0-9\-]+)/$', 'product.views.edit'),
url(r'^publish/(?P<product>[a-z0-9\-]+)/$', 'product.views.publish'),
url(r'^upload_image/(?P<product>[a-z0-9\-]+)/$', 'product.views.upload_image'),

# Blog
url(r'^(?P<product>[a-z0-9\-]+)/', include(extra_patterns)),
)

关于python - 对相似的 URL 模式进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10541383/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com