django Navigation,需要关于在哪里放置www.example.com文件的帮助tags.py

oug3syen  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(98)

我试图做Ben Liyanage在Django中使用标签处理导航的回答。这是我阅读Navigation in django的链接。
我试图在我的项目中使用它来学习如何在Django中进行导航。例如,当我打开我的login.html时,我得到了以下错误:“tags”不是有效的标记库:找不到模板库标签,已尝试django.templatetags.tags,django.contrib.admin.templatetags.tags,django.contrib.staticfiles.templatetags.tags
实际上,我不确定tags.py文件应该放在哪里。下面是我的文件路径和代码:

## file path
-- Main project folder
  -- manage.py
  -- .. other files ..
  -- src
     -- urls.py
     -- .. other files ..
  -- templates
     -- base.html
  -- app folder
    -- templates
       -- app name
          -- index.html
          -- login.html
          -- register.html
    -- tags.py
    -- .. other files ..

## urls.py
from django.conf.urls import patterns, url

from game import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^upload/$', views.upload_file, name='upload'),
    url(r'^successful_upload/$', views.successful_upload, name='successful_upload'),
    url(r'^play/$', views.play, name='play'),
    url(r'^registration/$', views.register, name='register'),
    url(r'^successful_registeration/$', views.successful_registeration, name='successful_registeration'),
    url(r'^login/$', views.login, name='login'),
    url(r'^logout/$', views.logout, name='logout')
)

## tags.py
from django import template

register = template.Library()

@register.tag
def active(parser, token):
    import re
    args = token.split_contents()
    template_tag = args[0]
    if len(args) < 2:
        raise template.TemplateSyntaxError, "%r tag requires at least one argument" % template_tag
    return NavSelectedNode(args[1:])

class NavSelectedNode(template.Node):
    def __init__(self, patterns):
        self.patterns = patterns
    def render(self, context):
        path = context['request'].path
        for p in self.patterns:
            pValue = template.Variable(p).resolve(context)
            if path == pValue:
                return "-active"
        return ""

## base.html
{% load tags %}
{% url 'index' as home %}
{% url 'upload' as upload %}
{% url 'play' as play %}
{% url 'register' as contact %}
{% url 'login' as login %}

<div id="navigation">
    <a class="{% active request home %}" href="{{ home }}">Home</a>
    <a class="{% active request upload %}" href="{{ upload }}">Upload</a>
    <a class="{% active request play %}" href="{{ play }}">Play</a>
    <a class="{% active request contact %}" href="{{ contact }}">Contact</a>
    <a class="{% active request login %}" href="{{ login }}">Login</a>
</div>

## login.html
{% extends "base.html" %}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/game/login/" method="post">
{% csrf_token %}
<table border='0'>
<div class="fieldWrapper"><tr><td>
        {{ form.user_name.errors }}</td><td></td></tr><tr><td>
        <label for="id_user_name">User Name:</label></td><td>
        {{ form.user_name }}</td></tr>
    </div>
    <div class="fieldWrapper"><tr><td>
        {{ form.password.errors }}</td><td></td><tr><td>
        <label for="id_password">Password:</label></td><td>
        {{ form.password }}</td></tr>
    </div>
</table>
<input type="submit" value="Login" />
</form>
vnzz0bqm

vnzz0bqm1#

你应该在app文件夹下创建一个templatetags包,并将tags.py放入其中:

project_folder/
    app_folder/
        __init__.py
        models.py
        templatetags/
            __init__.py
            tags.py
        views.py

不要忘记在templatetags目录中包含一个空的__init__.py文件,否则Python解释器会认为它是一个普通的目录,而不是Python包。详情请参见https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/

相关问题