为 URL 模式添加前缀

Posted

技术标签:

【中文标题】为 URL 模式添加前缀【英文标题】:Add a prefix to URL patterns 【发布时间】:2014-01-26 16:25:09 【问题描述】:

我有一个 Django 应用程序,其中包含多个 URL 模式条目 (urls.py):

urlpatterns = patterns(
    '',
    # change Language
    (r'^i18n/', include('django.conf.urls.i18n')),
    url('^api/v1/', include(router.urls)),
    url(r'^api-docs/', RedirectView.as_view(url='/api/v1/')),
    url(r'^api/', RedirectView.as_view(url='/api/v1/')),
    url(r'^api/v1', RedirectView.as_view(url='/api/v1/')),

    # django default stuff
    url(r'^accounts/', include('main.registration_urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # oath2_provider
    url(r'^o/', include('oauth2_provider.urls', namespace='oauth2_provider')),

    # google urls
    url(r'^gauthtest/$',
        'main.google_export.google_oauth2_request',
        name='google-auth'),
    url(r'^gwelcome/$',
        'main.google_export.google_auth_return',
        name='google-auth-welcome'),

    # main website views
    url(r'^$', 'main.views.home'),
    url(r'^tutorial/$', 'main.views.tutorial', name='tutorial'),
    url(r'^about-us/$', 'main.views.about_us', name='about-us'),
    url(r'^getting_started/$', 'main.views.getting_started', name='getting_started'),
    url(r'^faq/$', 'main.views.faq', name='faq'),
    url(r'^syntax/$', 'main.views.syntax', name='syntax'),
    url(r'^resources/$', 'main.views.resources', name='resources'),
    url(r'^forms/$', 'main.views.form_gallery', name='forms_list'),
    url(r'^forms/(?P<uuid>[^/]+)$', 'main.views.show'),
    url(r'^people/$', 'main.views.members_list'),
    url(r'^xls2xform/$', 'main.views.xls2xform'),
    url(r'^support/$', 'main.views.support'),
    url(r'^stats/$', 'staff.views.stats'),
    url(r'^login_redirect/$', 'main.views.login_redirect'),
    url(r"^attachment/$", 'odk_viewer.views.attachment_url'),
    url(r"^attachment/(?P<size>[^/]+)$", 'odk_viewer.views.attachment_url'),
    url(r'^jsi18n/$', 'django.views.i18n.javascript_catalog',
        'packages': ('main', 'odk_viewer',)),
    url(r'^typeahead_usernames', 'main.views.username_list', name='username_list'),
    url(r'^(?P<username>[^/]+)/$', 'main.views.profile', name='user_profile'),
    url(r'^(?P<username>[^/]+)/profile$', 'main.views.public_profile', name='public_profile'),
    url(r'^(?P<username>[^/]+)/settings', 'main.views.profile_settings'),
    url(r'^(?P<username>[^/]+)/cloneform$', 'main.views.clone_xlsform'),
    url(r'^(?P<username>[^/]+)/activity$', 'main.views.activity'),
    url(r'^(?P<username>[^/]+)/activity/api$', 'main.views.activity_api'),
    url(r'^activity/fields$', 'main.views.activity_fields'),
    url(r'^(?P<username>[^/]+)/api-token$', 'main.views.api_token'),

    # form specific
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)$', 'main.views.show'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/qrcode$', 'main.views.qrcode', name='get_qrcode'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/api$', 'main.views.api', name='mongo_view_api'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/public_api$', 'main.views.public_api', name='public_api'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/delete_data$', 'main.views.delete_data', name='delete_data'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/edit$', 'main.views.edit'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/perms$', 'main.views.set_perm'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/bamboo$', 'main.views.link_to_bamboo'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/photos', 'main.views.form_photos'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/doc/(?P<data_id>\d+)', 'main.views.download_metadata'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/delete-doc/(?P<data_id>\d+)', 'main.views.delete_metadata'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/formid-media/(?P<data_id>\d+)', 'main.views.download_media_data'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/submission/(?P<uuid>[^/]+)$', 'main.views.show_submission'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/addservice$', 'restservice.views.add_service', name="add_restservice"),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/delservice$',
        'restservice.views.delete_service', name="delete_restservice"),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/update$', 'main.views.update_xform'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/preview$', 'main.views.enketo_preview'),

    # briefcase api urls
    url(r"^(?P<username>\w+)/view/submissionList$",
        'odk_logger.views.view_submission_list'),
    url(r"^(?P<username>\w+)/view/downloadSubmission$",
        'odk_logger.views.view_download_submission'),
    url(r"^(?P<username>\w+)/formUpload$",
        'odk_logger.views.form_upload'),
    url(r"^(?P<username>\w+)/upload$",
        'odk_logger.views.form_upload'),

    # stats
    url(r"^stats/submissions/$", 'staff.views.submissions'),

    # exporting stuff
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.csv$", 'odk_viewer.views.data_export', name='csv_export', kwargs='export_type': 'csv'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.xls", 'odk_viewer.views.data_export', name='xls_export', kwargs='export_type': 'xls'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.csv.zip", 'odk_viewer.views.data_export', name='csv_zip_export', kwargs='export_type': 'csv_zip'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.kml$", 'odk_viewer.views.kml_export'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/data\.zip", 'odk_viewer.views.zip_export'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/gdocs$", 'odk_viewer.views.google_xls_export'),
    url(r"^odk_viewer/survey/(?P<instance_id>\d+)/$", 'odk_viewer.views.survey_responses'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/map_embed", 'odk_viewer.views.map_embed_view'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/map", 'odk_viewer.views.map_view'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/instance", 'odk_viewer.views.instance'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/enter-data", 'odk_logger.views.enter_data', name='enter_data'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/add-submission-with", 'odk_viewer.views.add_submission_with', name='add_submission_with'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/thank_you_submission", 'odk_viewer.views.thank_you_submission', name='thank_you_submission'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/edit-data/(?P<data_id>\d+)$", 'odk_logger.views.edit_data', name='edit_data'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/view-data", 'odk_viewer.views.data_view'),
    url(r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/new$", 'odk_viewer.views.create_export'),
    url(r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/delete$", 'odk_viewer.views.delete_export'),
    url(r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/progress$", 'odk_viewer.views.export_progress'),
    url(r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/$", 'odk_viewer.views.export_list'),
    url(r"^(?P<username>\w+)/exports/(?P<id_string>[^/]+)/(?P<export_type>\w+)/(?P<filename>[^/]+)$", 'odk_viewer.views.export_download'),

    # odk data urls
    url(r"^submission$", 'odk_logger.views.submission'),
    url(r"^(?P<username>\w+)/formList$", 'odk_logger.views.formList'),
    url(r"^(?P<username>\w+)/xformsManifest/(?P<id_string>[^/]+)$",
        'odk_logger.views.xformsManifest'),
    url(r"^(?P<username>\w+)/submission$", 'odk_logger.views.submission'),
    url(r"^(?P<username>\w+)/bulk-submission$", 'odk_logger.views.bulksubmission'),
    url(r"^(?P<username>\w+)/bulk-submission-form$", 'odk_logger.views.bulksubmission_form'),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/form\.xml$", 'odk_logger.views.download_xform', name="download_xform"),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/form\.xls$", 'odk_logger.views.download_xlsform', name="download_xlsform"),
    url(r"^(?P<username>\w+)/forms/(?P<id_string>[^/]+)/form\.json", 'odk_logger.views.download_jsonform', name="download_jsonform"),
    url(r"^(?P<username>\w+)/delete/(?P<id_string>[^/]+)/$", 'odk_logger.views.delete_xform'),
    url(r"^(?P<username>\w+)/(?P<id_string>[^/]+)/toggle_downloadable/$", 'odk_logger.views.toggle_downloadable'),

    # SMS support
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/sms_submission/(?P<service>[a-z]+)/?$', 'sms_support.providers.import_submission_for_form', name='sms_submission_form_api'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/sms_submission$', 'sms_support.views.import_submission_for_form', name='sms_submission_form'),
    url(r"^(?P<username>[^/]+)/sms_submission/(?P<service>[a-z]+)/?$", 'sms_support.providers.import_submission', name='sms_submission_api'),
    url(r'^(?P<username>[^/]+)/forms/(?P<id_string>[^/]+)/sms_multiple_submissions$', 'sms_support.views.import_multiple_submissions_for_form', name='sms_submissions_form'),
    url(r"^(?P<username>[^/]+)/sms_multiple_submissions$", 'sms_support.views.import_multiple_submissions', name='sms_submissions'),
    url(r"^(?P<username>[^/]+)/sms_submission$", 'sms_support.views.import_submission', name='sms_submission'),

    # static media
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
        'document_root': settings.MEDIA_ROOT),
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        'document_root': settings.MEDIA_ROOT),
    url(r'^favicon\.ico', RedirectView.as_view(url='/static/images/favicon.ico'))
)

我需要为每个 URL 添加一个前缀(例如 /myprefix/ ),而不是手动为每个条目添加前缀(编辑每一行)是否可以通过 Python 代码添加前缀?

【问题讨论】:

【参考方案1】:

创建一个查找通用 URL 前缀的模式,然后包含第二个模式对象:

urlpatterns = patterns(
    '',
    url(r'^myprefix/', include(patterns(
        '',
        # change Language
       (r'^i18n/', include('django.conf.urls.i18n')),
       url('^api/v1/', include(router.urls)),
       url(r'^api-docs/', RedirectView.as_view(url='/api/v1/')),
       url(r'^api/', RedirectView.as_view(url='/api/v1/')),
       url(r'^api/v1', RedirectView.as_view(url='/api/v1/')),
       # Et cetera
    )
)

事实上,您或许应该将所有以 api/ 开头的 URL 分组,绝对是所有以 r'^(?P&lt;username&gt;[^/]+)/forms/(?P&lt;id_string&gt;[^/]+)/' 开头的 URL。

编辑:我没有对此进行测试,请参阅“包含其他 URLconfs”下的 the documentation。

【讨论】:

嗨,我尝试使用前缀 = formhub 编写代码,但我得到 Page not found (404):使用 formhub.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:^formhub/当前 URL 与其中任何一个都不匹配。 我在找到文档后没有测试就写了。现在在家里,看看我编辑的链接。【参考方案2】:

我在 Django 文档中发现有一个 URL 前缀的用例。

在 Django 文档中:Including other URLconfs:

我们可以通过只声明一次公共路径前缀并将不同的后缀分组来改进这一点:

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

urlpatterns = [
    url(r'^(?P<page_slug>[\w-]+)-(?P<page_id>\w+)/', include([
        url(r'^history/$', views.history),
        url(r'^edit/$', views.edit),
        url(r'^discuss/$', views.discuss),
        url(r'^permissions/$', views.permissions),
    ])),
]

【讨论】:

也适用于 Django 1.9。谢谢【参考方案3】:

只需创建一个 django 应用并将所有当前代码移至该应用即可。最后将这个 urls.py 包含到项目的 urls.py 中。像这样

url(r'^myprefix/', include('app.urls')),

【讨论】:

【参考方案4】:

如果您只想为所有 URL 添加前缀,您可以在 urls.py 文件中使用以下代码:

from django.conf.urls import include
.
.
.
urlpatterns = [
... # Your URL patterns
]
# Add 'prefix' to all urlpatterns
urlpatterns = [url(r'^prefix/', include(urlpatterns))]

它会为所有 urlpatterns 添加前缀。

【讨论】:

【参考方案5】:

对于 django 2+ 现在是

from django.conf.urls import include
.
.
.
urlpatterns = [
... # Your URL patterns
]
# Add 'prefix' to all urlpatterns
urlpatterns = [path(r'^prefix/', include(urlpatterns))]

我将它与一个设置一起使用,以便我可以轻松地部署到不同的环境,这就是我的做法。

from django.conf.urls import include
from django.conf import settings
.
.
urlpatterns = [
... # Your URL patterns
]
# Add 'prefix' to all urlpatterns
if settings.URL_PREFIX:
    urlpatterns = [path(f'settings.URL_PREFIX/', include(urlpatterns))]

URL_PREFIX 也必须在设置中指定。

【讨论】:

效果很好。但是你使用国际化吗?它不断产生错误。此外,它工作的情况会产生一个模式,我有/fr/URL_PREFIX/,而我想要/URL_PREFIX/fr/ 我没有,但您可以通过使用i18n_patterns 包装 URL 前缀路径来获得相同的结果,这是文档。 docs.djangoproject.com/en/3.0/topics/i18n/translation/… 每当我在第一个 urlpatterns 上应用内部化并在 urlpatterns = [path(f'settings.URL_PREFIX/', include(urlpatterns))] 中使用它时,它都会产生一个错误,指出 Using i18n_patterns in an included URLconf is not allowed. 这就是我想要的。前缀出现在语言指示之前,例如/cute_prefix/fr/

以上是关于为 URL 模式添加前缀的主要内容,如果未能解决你的问题,请参考以下文章

重复第 n 个数字直到第 n 个匹配,同时使用 awk sub 在模式中为这些数字添加前缀

Flask---蓝图

vim 列编辑模式

在运行时更新log4j2模式

为所有从包路由导入的前缀添加前缀时,如何在 Symfony 包的模板中生成 URL?

在没有前缀的 pg_temp 模式中调用函数