简体   繁体   English

导航栏中的 Django 上下文变量 - 正确的 href 分配?

[英]Django context variable in the navigation bar - correct href assignment?

What is the correct assignment of context variables in the navigation bar?导航栏中上下文变量的正确分配是什么? My Django example is:我的 Django 示例是:

in view.py :view.py中:

from django.shortcuts import render
from django.http import HttpResponse 

context_navigation =   {
           'Link1'    : 'Blog',
           'href1'    : "{% url 'blog' %}",   }

def index(request):
    return render(request, 'app_about/index.html', context=context_navigation)  
    

in urls.py :urls.py中:

from django.urls import path
from . import views

urlpatterns = [ 
    path('blog/', views.index, name='blog'),  
    ]
    

in templates/base.html this does worktemplates/base.html这确实有效

<a class="nav-item nav-link text-light"  href="{% url 'blog' %}" > {{Link1}}</a>

this does not work (see href1 and Link1 )不起作用(参见href1Link1

<a class="nav-item nav-link text-light"  href="{{href1}}" > {{Link1}}</a>

In the last case a wrong url is generated, something like http://127.0.0.1:8000/blog/%7B% .在最后一种情况下,会生成错误的 url,例如http://127.0.0.1:8000/blog/%7B% What is the correct assignment of href as a context variable href1 ?href作为上下文变量href1的正确分配是什么? Thank you for some hints!谢谢你的一些提示!

In the second example you insert a variable href1 , which is replaced by the content (the string) {% url 'blog %} when rendered.在第二个示例中,您插入了一个变量href1 ,它在呈现时被内容(字符串) {% url 'blog %} Django trys to render this string (makes it html safe, thats where the %7B .. came from, which is just the html code for { . Django 尝试渲染此字符串(使其 html 安全,这就是%7B .. 的来源,这只是 html 代码{

Option 1: resolve url in the view选项1:在视图中解析url

You can either resolve the url using python in the view and pass the actual url as string to the template:您可以在视图中使用 python 解析 url 并将实际的 url 作为字符串传递给模板:

views.py视图.py

from django.urls import reverse

context_navigation =   {
       'Link1'    : 'Blog',
       'href1'    : reverse('blog')
}

base.html底座.html

<a class="nav-item nav-link text-light"  href="{{href1}}">{{Link1}}</a>

Option 2: resolve url in the template选项 2:在模板中解析 url

Or you go with the first example, where {% url 'blog %} will be interpreted and executed by the template rendering engine:或者你 go 与第一个例子,其中{% url 'blog %}将由模板渲染引擎解释和执行:

context_navigation =   {
       'Link1'    : 'Blog'
}

base.html底座.html

<a class="nav-item nav-link text-light"  href="{% url 'blog' %}">{{Link1}}</a>

Option3: view passes url (name) and template resolves it (not tested)选项3:视图通过url(名称)和模板解决它(未测试)

If you need to generate the context dynamically but want to resolve the actual urls in the template you can maybe pass the url name as variable like:如果您需要动态生成上下文但想要解析模板中的实际 url,您可以将 url 名称作为变量传递,例如:

context_navigation =   {
       'Link1': 'Blog',
       'url1': 'blog',
}

base.html底座.html

<a class="nav-item nav-link text-light"  href="{% url url1 %}">{{Link1}}</a>

But I'm not sure if you can use varaibles in template tags like this (just a guess, never used it this way)但我不确定您是否可以在这样的模板标签中使用变量(只是猜测,从未以这种方式使用过)

Thank you for the answer @sarbot.谢谢@sarbot 的回答。 Let's give me my experiences with them.让我们谈谈我与他们相处的经历。

Option 1 looks pretty lean and is in accordance with the django manual, so I would prefer it as the solution.选项 1看起来很精简,并且符合 django 手册,所以我更喜欢它作为解决方案。 Unfortunately I have to struggle with the fact that runserver does not like this approach and generates a fatal Python error.不幸的是,我不得不为 runserver 不喜欢这种方法并产生致命的 Python 错误而苦苦挣扎。 For the time beeing I do not have an explanation for that.暂时我对此没有任何解释。

Option 2 is the option I started with and it works.选项 2是我开始使用的选项,它有效。 For a clean programming style I prefere not to stay with it.对于干净的编程风格,我不喜欢继续使用它。

Option 3 works for me , so I do not have to say more than thank you:-)选项 3 对我有用,所以我不必多说谢谢:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM