Add a prefix to URL patterns in Django?

后端 未结 5 1103
长发绾君心
长发绾君心 2020-12-15 22:50

I have a DJango application that have several entries in URL patterns (urls.py):

urlpatterns = patterns(
    \'\',
    # change Language
    (r\'^i18n/\', in         


        
5条回答
  •  鱼传尺愫
    2020-12-15 23:17

    Make a pattern that looks for the common URL prefix, then include a second patterns object:

    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
        )
    )
    

    In fact you should perhaps group all the URLs that start with api/ this way, and definitely all the ones that start with r'^(?P[^/]+)/forms/(?P[^/]+)/'.

    Edit: I didn't test that, see the documentation under "Including other URLconfs".

提交回复
热议问题