0

我一直在尝试解决这个问题一段时间,但一直未能找到有效的解决方案。也许有人可以帮助指出更好的方法!

我正在使用我的根 urlconf 将两个请求重定向到包含的应用程序:

url("^about/news/", include("kdi.urls")),
url("^about/recognition/", include("kdi.urls")),

这是kdi应用程序的 urlconf:

url(r"^$", "kdi.views.news", name="news"),

# this is the pattern that needs to change:
url(r"^$", "kdi.views.recog", name="recog"),

^about/news/使用从 root到应用程序 urlconf^about/recognition的更精细的重定向似乎更聪明。^$这仅适用于一种模式,但我想将其扩展为适用于两种模式。

是否会像^about/从根目录定向到我可以检查的应用程序^/news$^/recognition$kdi应用程序中更智能?^如果没有匹配,那是否也可以使用根的包罗万象?是否可以request.path从 urlconf 检查然后使用 if 语句指向正确的视图?或者也许name在根目录中使用一个字段,然后在应用程序的 url 模式中通过该名称访问?

只是在解决这个逻辑时遇到了一些麻烦!

编辑:

从根 urlconf 中删除namespace字段以限制混淆

4

1 回答 1

3

你在这里做了一些非常奇怪的事情,我不知道为什么。

您应该只包含一次应用 URL。但是包含文件中的每个 URL 都需要不同 - 否则 Django 不可能知道如何路由请求。

所以,主要的 urls.py 应该只是:

url("^about/", include("kdi.urls")),

应用程序应该是:

url(r"^news/$", "kdi.views.news", name="news"),
url(r"^recognition/$", "kdi.views.recog", name="recog")
于 2017-07-10T21:27:46.363 回答