Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 命名空间url解析_Python_Django_Django Templates - Fatal编程技术网

Python 命名空间url解析

Python 命名空间url解析,python,django,django-templates,Python,Django,Django Templates,我正在努力让django工作,因为我希望我的老板和他的老板能够从一个只填充虚假数据而实际上什么都不做的应用程序中看到模板并对其进行评论,而我在另一个应用程序中使用相同的模板开发实时代码。因此,如果您转到http://localhost:8000/template_test/base您会看到base.html模板中包含虚假数据,如果转到http://localhost:8000/uar/base您可以看到base.html包含(希望是)真实数据。更复杂的是,页面上有一个链接,该链接应该指向uar.

我正在努力让django工作,因为我希望我的老板和他的老板能够从一个只填充虚假数据而实际上什么都不做的应用程序中看到模板并对其进行评论,而我在另一个应用程序中使用相同的模板开发实时代码。因此,如果您转到
http://localhost:8000/template_test/base
您会看到base.html模板中包含虚假数据,如果转到
http://localhost:8000/uar/base
您可以看到base.html包含(希望是)真实数据。更复杂的是,页面上有一个链接,该链接应该指向uar.html,其中包含虚假数据或真实数据,具体取决于您是访问了/template\u test/base url还是/uar/base url

下面是模板的适当部分:

<li>
  <a href="{% url 'uar:uar' %}">User Access Review</a>
</li>
在模板_test/url.py中

urlpatterns = patterns('',
  url(r'^base$', template_test.views.base, name="base"),
  url(r'^uar$', template_test.views.uar_missing, name="uar"),
和在uar/url.py中

urlpatterns = patterns('',
  url(r'^base$', uar.views.base, name="base"),
  url(r'^uar$', uar.views.uar_missing, name="uar"),
模板_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\u test/uar/
({full\u name}}具有相应的值,分别是“假用户”或“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'
显然,我混淆了“名称空间”和“应用程序名称”。My project/URL.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")),
注意:我已经切换了名称空间和应用程序名称

模板_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中传递当前的_应用程序

另外,我还发现,当我使用重定向时,我需要使用正确的当前应用程序进行“反向”

def base(request):
    return render(request, "base.html", {"full_name": "Paul Tomblin"})