使用Django模板的导航菜单 [英] Navigation menu using Django templates
问题描述
尝试使用Django模板处理琐碎的导航菜单时,我无法在特定菜单项上设置当前类。这是我的基本模板:
Trying to work with a trivial navigation menu using django templates, I'm having trouble setting the current class on a particular menu item. Here's my base template:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{% block title %}{% endblock %}</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/media/css/base.css" />
<link rel="stylesheet" type="text/css" href="/media/css/login.css" />
<link rel="stylesheet" href="/site_media/css/style.css" type="text/css" />
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="/media/css/ie.css" /><![endif]-->
</head>
<body class="{% block bodyclass %}{% endblock %}">
{% block content %}{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
然后我有一个nav.html:
Then I have a nav.html:
<ul id="top">
<li><a class="{% block home %}{% endblock %}" href="/">Home</a></li>
<li><a class="{% block myaccount %}{% endblock %}" href="/profile/">My Account</a></li>
{% if perms.staffing.add_staffrequest %}
<li><a class="{% block createsr %}{% endblock %}"
href="/create/staffrequest/">Staff Request</a></li>
{% endif %}
</ul>
现在在我的home.html中,我似乎无法显示当前的类:
And now in my home.html, I can't seem to get the class current to display:
{% extends "base.html" %}
{% block title %}Home Portal{% endblock %}
{% block content %}
<div id="content">
{% include "nav.html" %}
{% block home %}current{% endblock %}
<div id="intro">
<p>Hello, {{ user.first_name }}.</p>
<p>Please create a Staff Request here by filling out the form
below.</p>
</div> <!-- end intro -->
<div id="logout">
<a href="/accounts/logout" alt="Sign Off" title="Sign Off">Sign Off</a>
</div>
</div> <!-- end content -->
{% endblock %}
当前类不是在导航中显示适当的元素,让我根据用户所处的页面来为其设置视觉上下文。
The class 'current' isn't showing up in navigation for the appropriate element, letting me set visual context for a user depending what page they're on.
推荐答案
我认为您不能从包含的模板中替换一个块。我的建议是,您需要重新考虑模板的逻辑。恕我直言,应该是这样的:
I don't think you can replace a block from an included template. My suggestion is that you need to rethink the logic of your templates. IMHO it should be something like this:
base.html
base.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{% block title %}{% endblock %}</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="/media/css/base.css" />
<link rel="stylesheet" type="text/css" href="/media/css/login.css" />
<link rel="stylesheet" href="/site_media/css/style.css" type="text/css" />
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="/media/css/ie.css" /><![endif]-->
</head>
<body class="{% block bodyclass %}{% endblock %}">
{% block content %}
<div id="content">
{% block navigation %}
<ul id="top">
<li><a class="{% block home %}{% endblock %}" href="/">Home</a></li>
<li><a class="{% block myaccount %}{% endblock %}" href="/profile/">My Account</a></li>
{% if perms.staffing.add_staffrequest %}
<li><a class="{% block createsr %}{% endblock %}"
href="/create/staffrequest/">Staff Request</a></li>
{% endif %}
</ul>
{% endblock %}
{% block real_content %}
<div id="intro">
<p>Hello, {{ user.first_name }}.</p>
<p>Please create a Staff Request here by filling out the form below.</p>
</div> <!-- end intro -->
<div id="logout">
<a href="/accounts/logout" alt="Sign Off" title="Sign Off">Sign Off</a>
</div>
{% endblock %}
</div> <!-- end content -->
{% endblock %}
{% block footer %}{% endblock %}
</body>
</html>
,而您的home.html应该看起来像
and your home.html should look like
{% extends "base.html" %}
{% block title %}Home Portal{% endblock %}
{% block home %}current{% endblock %}
{% block real_content %}
<div id="content">
<div id="intro">
<p>Hello, {{ user.first_name }}.</p>
<p>Please create a Staff Request here by filling out the form
below.</p>
</div> <!-- end intro -->
<div id="logout">
<a href="/accounts/logout" alt="Sign Off" title="Sign Off">Sign Off</a>
</div>
</div> <!-- end content -->
{% endblock %}
这篇关于使用Django模板的导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!