[英]Jython ImportError: No module named gargoylesoftware, no resolution found
[英]Named space url resolution
我正在尝试使django 命名空间的url解析正常工作,因为我希望我的老板和他的老板能够从一个应用程序中看到这些模板并对其进行评论,而该应用程序只会填充假数据,而实际上却什么也没做在不同应用中开发实时代码时使用相同的模板。 因此,如果您访问http://localhost:8000/template_test/base
则会看到带有假数据的base.html模板;如果您访问http://localhost:8000/uar/base
则会看到base.html (希望)真实数据。 更为复杂的是,页面上的链接应该转到uar.html,其中包含虚假数据或真实数据,具体取决于您转到的是/ template_test / base url还是/ uar / base url。
因此,这是模板的适当部分:
<li>
<a href="{% url 'uar:uar' %}">User Access Review</a>
</li>
这是我的project / urls.py的适当部分
url(r'^template_test/', include(template_test.urls,
namespace="uar", app_name="template_test")),
url(r'^uar/', include(uar.urls, namespace="uar", app_name="uar")),
并在template_test / urls.py中
urlpatterns = patterns('',
url(r'^base$', template_test.views.base, name="base"),
url(r'^uar$', template_test.views.uar_missing, name="uar"),
并在uar / urls.py中
urlpatterns = patterns('',
url(r'^base$', uar.views.base, name="base"),
url(r'^uar$', uar.views.uar_missing, name="uar"),
template_test / views.py
def base(request):
return render(request, "base.html", {"full_name": "Fake User"},
current_app="template_test")
和uar / views.py
def base(request):
return render(request, "base.html", {"full_name": "Paul Tomblin"},
current_app="uar")
def uar_missing(request):
return render(request, "uar.html", {}, current_app="uar")
但是,尽管我正在为模板提供应用程序上下文,但是当在任一上下文中呈现base.html时,模板中的{% url 'uar:uar' %}
最终还是/template_test/uar/
在两种情况下({{full_name}}都具有适当的值,分别是“ Fake User”或“ Paul Tomblin”)。 为了使该链接使用当前应用程序上下文,我需要更改什么?
附加信息应用程序上下文不适用于反向操作:
python manage.py shell
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.urlresolvers import reverse
>>> reverse('uar:uar')
'/template_test/uar'
>>> reverse('uar:uar', current_app='uar')
'/template_test/uar'
>>> reverse('uar:uar', current_app='template_test')
'/template_test/uar'
>>> reverse('uar:uar', current_app='txx')
'/template_test/uar'
显然我在混淆“命名空间”和“ app_name”。 我的project / urls.py现在看起来像:
url(r'^template_test/',
include(build_urls(template_test.views, 'template_test'),
namespace="template_test", app_name="uar")),
url(r'^uar/',
include(build_urls(uar.views, 'uar'),
namespace="uar", app_name="uar")),
注意:我已经切换了名称空间和app_name。
template_test / views.py具有
def base(request):
return render(request, "base.html", {"full_name": "Fake User"},
current_app="template_test")
和uar / views.py有
def base(request):
return render(request, "base.html", {"full_name": "Paul Tomblin"})
注意:我不再在uar / views.py中传递current_app了。
另外,我还发现使用重定向时,需要对正确的current_app进行“反向”操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.