Django {% url %} when urls with parameters like: url(r'^foo//$', include(some.urls))

后端 未结 2 1276
甜味超标
甜味超标 2021-01-19 06:25

i don\'t find any solution, how to get the url in template with the following configuration (using Django1.3):

urls.py

urlpatterns = pattern         


        
相关标签:
2条回答
  • 2021-01-19 06:41

    Well, I usually set namespace for included urls to simplify the process:

    in root urlpatterns

    url(r'^articles/', include('articles.urls', namespace='articles')),

    in articles/urls.py

    url(r'^(\d+)/$', 'read', name='read'),

    url(r'^publish/$', 'publish', name='publish'),

    And then in your template you can simply type:

    {% url articles:read 1 %}

    or

    {% url articles:publish %}

    More about this here.

    0 讨论(0)
  • 2021-01-19 06:56

    Yes, you can get your url like this:-

    {% url 'bar-url' 1 2 %}
    

    But note that your url configuration should actually be like this:-

    urls.py

    urlpatterns = patterns('',
        url(r'^/foo/(?P<parameter>\d+)/, include('bar.urls')),
    )
    

    bar.urls.py

    urlpatterns = patterns('',
        (r'^/bar/$, 'bar.views.index'),
        url(r'^/bar/(?P<parameter2>\d+)/$, 'bar.views.detail', name='bar-url'),
    )
    

    There is no foo-url unless you specifically map:-

    urls.py

    urlpatterns = patterns('',
        url(r'^/foo/(?P<parameter>\d+)/$, 'another.views.foo', name='foo'),
        url(r'^/foo/(?P<parameter>\d+)/, include('bar.urls')),
    )
    

    Note that $ means the end of the regular expression.

    0 讨论(0)
提交回复
热议问题