1

<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>それを前処理するためにすべてのパターンの周りに置くことができるある種のラッパーはありますか?

4

1 に答える 1

2

こんにちは、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)),
)
于 2012-05-10T20:36:38.170 に答える