django urlconf 直接从根目录到包含的应用程序逻辑
django urlconf direct from root to included app logic
一段时间以来,我一直在尝试解决这个问题,但一直未能找到有效的解决方案。或许有人可以帮忙指出更好的方法!
我正在使用我的根 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"),
在应用程序的 urlconf 中使用从根 ^about/news/
和 ^about/recognition
到 ^$
的更精细的重定向似乎更明智。这只适用于一种模式,但我想扩展它以适用于两种模式。
将 ^about/
从 root 引导到我可以在 kdi
应用程序中检查 ^/news$
或 ^/recognition$
的应用程序会更智能吗?如果没有匹配,那是否也可以使用 root 的 catch-all ^
?是否可以从 urlconf 检查 request.path
然后使用 if 语句指向正确的视图?或者可以在根目录中使用 name
字段,然后在应用程序的 url 模式中通过该名称进行访问?
只是在理解这个逻辑上有点麻烦!
编辑:
从根 urlconf 中删除了 namespace
字段以限制混淆
你在这里做的事情很奇怪,我不明白为什么。
您应该只包含您的应用 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")
一段时间以来,我一直在尝试解决这个问题,但一直未能找到有效的解决方案。或许有人可以帮忙指出更好的方法!
我正在使用我的根 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"),
在应用程序的 urlconf 中使用从根 ^about/news/
和 ^about/recognition
到 ^$
的更精细的重定向似乎更明智。这只适用于一种模式,但我想扩展它以适用于两种模式。
将 ^about/
从 root 引导到我可以在 kdi
应用程序中检查 ^/news$
或 ^/recognition$
的应用程序会更智能吗?如果没有匹配,那是否也可以使用 root 的 catch-all ^
?是否可以从 urlconf 检查 request.path
然后使用 if 语句指向正确的视图?或者可以在根目录中使用 name
字段,然后在应用程序的 url 模式中通过该名称进行访问?
只是在理解这个逻辑上有点麻烦!
编辑:
从根 urlconf 中删除了 namespace
字段以限制混淆
你在这里做的事情很奇怪,我不明白为什么。
您应该只包含您的应用 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")