Django - 在base.html中使用模型的全球导航 [英] Django - Global navigation in base.html using model

查看:186
本文介绍了Django - 在base.html中使用模型的全球导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些扩展 base.html 的模板。我想要使​​用 base.html 模板来放置我的全球导航,并将全局导航中的文本和链接基于模型部门(即​​ CharField 在模型中将被用作全局导航中的按钮文本,并且id将用于构建URL)。我认为标签可能正常工作,但我最终的结论是(是的,我刚刚接触Django和Python):



current_tags.py

 从django导入模板
#从libs.display.models导入部分导入您的模型

from django.db import models

register = template.Library()
@ register.simple_tag
def do_get_divisions(self):
d = Division。对象()
mylist = []
每个d:
mylist.append(str(each.DivisionValue))
返回我的列表

我正在尝试在每个对象中获取文本值,以便在这一点上打印,因为我无法获取或使用对象在模板中,如下所示。



base.html

 <!DOCTYPE html> 
< html>
< head>
{%load staticfiles%}
< link rel =stylesheettype =text / csshref ={{STATIC_URL}} style.css/>
< / head>
< body>
{%load current_tags%}
< p> {%do_get_divisions%}< / p> **这个实际打印mylist **

{do_get_divisions%}中的每个** **这不打印任何**
< p> {{each}}< / p为H.
{%endfor%}
{%block content%}
{%endblock%}
< / body>
< / html>

我确定有一个更好的方法来做基于Django中的模型的全球导航。我基本上想要得到所有的Division对象,并将它们放在一个< ul> 中,作为我的全球导航在 base.html 中使用。我不熟悉Django,但是我的 views.py 不能帮助我,因为我正在渲染其他模板,而不是 base.html base.html文件。对于什么是值得的,这里是一个 views.py ,其中 /display/info.html 模板扩展了 base.html

 #在这里创建您的意见。 
from django.http import httpResponse
from apps.pulldata.models import data
from django.shortcuts import render,get_object_or_404
from django.http import Http404

def info(request,group_id):
group = get_object_or_404(Data,pk = group_id)
s = group.XInGroup.all()
return render(request,'display / info。 html',{'Group':group,'s:s})


解决方案

您不能将模板标签放入另一个。你的for-loop是一个模板,希望你的上下文中的一个元素的名称迭代。



如果你想处理模板标签中的导航,你应该考虑使用包含标签



包含标签是使用模板呈现其数据的函数。



一个非常基本的实现可能如下所示: p>

tags.py

  @ register.inclusion_tag('navigation.html' )
def navigation(selected_id = None):
return {
'navigation':Division.objects.all(),
'selected':selected_id,
}

在您的模板标签文件中,您可以创建一个包含导航项目的字典,以及可选的当前所选项目以突出显示导航元素



navigation.html

 < ul> 
{%导航中的项目%}
< li {%if item.id == selected%} class =selected{%endif%}>
< a href ={{item.get_absolute_url}}> {{item.DivisionValue}}< / a>
< / li>
{%endfor%}
< / ul>

navigation.html使用python函数中的字典作为上下文,所以你只需简单地迭代导航。



base.html

  {%navigation%} 

  {%navigation current_division.id%} 

在base.html中,您调用包含标记像一个普通的模板标签。如果要突出显示当前项目,则将其ID添加为参数。


I have a number of templates that extend base.html. I want the base.html template to house my global navigation and have the text and links in the global navigation be based on a model Division (i.e. the CharField in the model will be used as the button text in the global nav, and the id will be used to build the URL). I thought tags might work, but what I end up with is this (yes, I'm new to Django and Python):

current_tags.py

from django import template
# Import your model
from libs.display.models import Division
from django.db import models

register = template.Library()
@register.simple_tag
def do_get_divisions(self):
    d = Division.objects.all()
    mylist = []
    for each in d:
        mylist.append(str(each.DivisionValue))
    return my list

I'm attempting just getting the text value in each object to print at this point because I can't get or work with the objects in the template, as you'll see below.

base.html

<!DOCTYPE html>
<html>
<head>
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}style.css" />
</head>
<body>
{% load current_tags %}
<p>{% do_get_divisions "" %}</p> **THIS ACTUALLY PRINTS mylist**

{% for each in do_get_divisions %} **THIS DOESN'T PRINT ANYTHING**
    <p>{{ each }}</p>
{% endfor %}  
{% block content %}
{% endblock %}
</body>
</html>

I'm sure there is a better way to do global nav based on a model in Django. I basically want to get all the Division objects and put them into a <ul> to use as my global nav in base.html. I am not that familiar with Django, but my views.py don't help me because I am rendering other templates, not base.html, which are extending base.html. For what it's worth, here's one views.py, where /display/info.html template extends base.html:

# Create your views here.
from django.http import HttpResponse
from apps.pulldata.models import Data
from django.shortcuts import render, get_object_or_404
from django.http import Http404

def info(request, group_id):
    group = get_object_or_404(Data, pk=group_id)
    s = group.XInGroup.all()
    return render(request, 'display/info.html', {'Group': group, 's': s})

解决方案

You cannot put a templatetag into another. Your for-loop is a templatetag that expects the name of an element in your context to iterate over.

If you want to handle the navigation in a template tag you should consider using inclusion tags.

Inclusion tags are functions that use templates to render their data.

A very basic implementation could look something like this:

tags.py

@register.inclusion_tag('navigation.html')
def navigation(selected_id=None):
    return {
        'navigation': Division.objects.all(),
        'selected':selected_id,
    }

In your templatetag file you create a dictionary with the navigation items and optionally the currentl selected item to highlight this navigation element.

navigation.html

<ul>
{% for item in navigation %}
  <li{% if item.id == selected %} class="selected"{% endif %}>
    <a href="{{ item.get_absolute_url }}">{{ item.DivisionValue }}</a>
  </li>
{% endfor %}
</ul>

the navigation.html uses the dictionary from the python function as context so you start with simply iterating over the navigation.

base.html

{% navigation %}

or

{% navigation current_division.id %}

In the base.html you call the inclusion tag like a normal template tag. if you want to highlight the current item you add its id as a argument.

这篇关于Django - 在base.html中使用模型的全球导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆