如何在模板上使用动态url

2024-10-01 02:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python2.7.11和django 1.10.2。我创建了url并使用动态模板。在

网址.py

url(r'^suits-anarkali/', include([
                url(r'^$', category_page, name="suits-anarkali"),
                url(r'^(?P<slug>[\w-]+)/$', category_page, name="suits-anarkali"),
                url(r'^(?P<slug>[\w-]+)/$', single_product, name='singleproduct'),
                url(r'^(?P<slug>[\w-]+)/(?P<singleproduct_slug>[\w-]+)/$', singleproduct, name="singleproduct"),
    ])
),

这是我的url结构。如果我用slug调用category_页面,那么它工作正常,但是当我用slug调用single_product时,它会重定向到category page。 我试了很多次,但没用。那么如何在模板上管理url呢?在

产品.html

^{pr2}$

Tags: djangonamepy模板urlpage动态product
1条回答
网友
1楼 · 发布于 2024-10-01 02:19:30

url在第一个匹配regexp的基础上工作,由于下面两行完全匹配相同的slug参数,所以当您使用slug调用url时,category_page首先匹配,因此django不会继续检查下一行。您需要对category_page进行一些更改,使其与其他regexp不同。在

    url(r'^(?P<slug>[\w-]+)/(?P<product>[\w-]+)/$', single_product, name='singleproduct'),
    url(r'^(?P<slug>[\w-]+)/$', category_page, name="suits-anarkali"),

read more on url tag

相关问题 更多 >