重复模式,不同的名称包含在django url中

时间:2022-07-18 04:36:17

I'm struggling with this problem in a Django app: My main app urls file has this two lines:

我在Django应用程序中遇到了这个问题:我的主应用程序url文件有两行:

url(r'^root-pattern-1/', include('loan.urls')),
url(r'^root-pattern-2/', include('loan.urls')),

And in my loan.urls I have the following two entries:

在我的贷款。url我有以下两个条目:

url(r'^search/$', Search.as_view(),
            name='personal-loan-result', kwargs={'loantype': LOAN_TYPE_PERSONAL}),
url(r'^search/$', Search.as_view(loantype=LOAN_TYPE_CREDIT),
            name='creditcard-loan-result', kwargs={'loantype': LOAN_TYPE_CREDIT}),

The problem is that when I call reverse('creditcard-loan-result') the url seems ok, but it's calling the url named 'personal-loan-result', that is the first entry. I've been reading a lot and saw in other questions here that the options are including my loan.urls file with empty pattern or changing the order of the url entries. Is there some other option that I am missing? changing the order will not work in this specific case and I don't like the idea of including the urls with empty pattern.

问题是,当我调用reverse('creditcard-loan-result')时,url看起来没问题,但它调用了名为“个人贷款结果”的url,这是第一个条目。我读了很多书,在其他问题中也看到了选择包括我的贷款。具有空模式的url文件或更改url条目的顺序。我还有什么别的选择吗?在这种特定的情况下,改变顺序是行不通的,我不喜欢包含带有空模式的url。

1 个解决方案

#1


1  

It's not clear to me why you are including loan.urls twice, so apologies if I've misunderstood your question.

我不清楚你为什么把贷款包括在内。url两次,抱歉,如果我误解了你的问题。

url(r'^search/$', Search.as_view(),
        name='personal-loan-result', kwargs={'loantype': LOAN_TYPE_PERSONAL}),
url(r'^search/$', Search.as_view(loantype=LOAN_TYPE_CREDIT),
        name='creditcard-loan-result', kwargs={'loantype': LOAN_TYPE_CREDIT}),

These are both for the same url, /search/. It doesn't matter whether you call reverse('personal-loan-result') or reverse('creditcard-loan-result'), the url that is displayed in the browser is simply /search/, and Django will always use the first url pattern that matches.

它们都是相同的url /search/。无论您调用reverse(“personal-loan-result”)还是reverse(“creditcard-loan-result”),在浏览器中显示的url都是/search/, Django总是使用第一个匹配的url模式。

If you want to direct results to the second pattern, then you need two different regexes, for example you could use ^search/personal/$ and ^search/credit/$.

如果你想直接结果第二模式,那么你需要两个不同的正则表达式,例如你可以使用搜索/个人/ ^ ^搜索/信贷/美元。

#1


1  

It's not clear to me why you are including loan.urls twice, so apologies if I've misunderstood your question.

我不清楚你为什么把贷款包括在内。url两次,抱歉,如果我误解了你的问题。

url(r'^search/$', Search.as_view(),
        name='personal-loan-result', kwargs={'loantype': LOAN_TYPE_PERSONAL}),
url(r'^search/$', Search.as_view(loantype=LOAN_TYPE_CREDIT),
        name='creditcard-loan-result', kwargs={'loantype': LOAN_TYPE_CREDIT}),

These are both for the same url, /search/. It doesn't matter whether you call reverse('personal-loan-result') or reverse('creditcard-loan-result'), the url that is displayed in the browser is simply /search/, and Django will always use the first url pattern that matches.

它们都是相同的url /search/。无论您调用reverse(“personal-loan-result”)还是reverse(“creditcard-loan-result”),在浏览器中显示的url都是/search/, Django总是使用第一个匹配的url模式。

If you want to direct results to the second pattern, then you need two different regexes, for example you could use ^search/personal/$ and ^search/credit/$.

如果你想直接结果第二模式,那么你需要两个不同的正则表达式,例如你可以使用搜索/个人/ ^ ^搜索/信贷/美元。