Django 自定义包含标签 [英] Django Custom Inclusion Tags

查看:30
本文介绍了Django 自定义包含标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建自己的模板标签.我不知道为什么我会收到这些错误.我正在关注 Django 文档.

I'm trying to build my own template tags. I have no idea why I getting these errors. I'm following the Django doc's.

这是我的应用程序的文件结构:

This is my app's file structure:

pollquiz/
    __init__.py
    show_pollquiz.html
    showpollquiz.py

这是 showpollquiz.py:

This is showpollquiz.py:

from django import template
from pollquiz.models import PollQuiz, Choice
register = template.Library()

@register.inclusion_tag('show_pollquiz.html')
def show_poll():
    poll = Choice.objects.all()
    return { 'poll' : poll }

html 文件:

<ul>
{% for poll in poll 
    <li>{{ poll.pollquiz }}</li>
{% endfor 
</ul>

在我的 base.html 文件中,我包含这样的内容

in my base.html file im am including like this

{% load showpollquiz %}
and
{% poll_quiz %}

然后我得到错误:

Exception Value: Caught an exception while rendering: show_pollquiz.html

我不知道为什么会发生这种情况.有任何想法吗?请记住,我还是 Django 的新手

I have no idea why this happens. Any ideas? Please keep in mind I'm still new at Django

推荐答案

不是所有自定义过滤器都应该在 templatetags 目录中吗?

Shouldn't all custom filters be inside the templatetags directory?

templatetags/
    __init__.py
    showpollquiz.py

然后

@register.inclusion_tag('show_pollquiz.html')

在 MY_TEMPLATE_DIR/show_pollquiz.html 中查找模板

looks in MY_TEMPLATE_DIR/show_pollquiz.html for the template

这篇关于Django 自定义包含标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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