简体   繁体   English

如何为现有Django应用添加自定义标签/过滤器?

[英]How to add a custom tag/filter to an existing Django app?

I have to adapt a Django application (first time working with Django) and I'm running into issues getting a custom tag/filter to work. 我必须改编Django应用程序(第一次使用Django),并且遇到了使自定义标签/过滤器正常工作的问题。 The app I'm working with has the following structure: 我正在使用的应用程序具有以下结构:

tool
|- manage.py
|- templates
|- ...
|- vote
    |- initadmin
    |- initproc
        |- templatetags
            | __init__.py
            | guard.py
        |- __init__.py
        |- apps.py
        |- models.py
        |- views.py
    |- __init__.py
    |- urls.py
    |- settings.py
    |- __pycache__
    |- wsgi.py

In settings.py my installed apps contains: settings.py中,我安装的应用程序包含:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',

    # 3rd party
    ...

    # must be before admin ...
    'django.contrib.admin',

    # locally
    'vote.initadmin',
    'vote.initproc',
]

I wanted to add a custom tag, which I could load from within templates {% load site_defaults %} to add certain default parameters to templates. 我想添加一个自定义标签,可以从模板{% load site_defaults %}该标签,以向模板添加某些默认参数。 From the docs I understand, that I would have to: 根据我了解的文档 ,我将必须:

1) add a module to templatetags , so I created 1)将一个模块添加到templatetags ,所以我创建了

# /vote/initproc/templatetags/site_defaults.py
import os
from django.conf import settings
from django import template
from six.moves import configparser

site_parser = configparser.ConfigParser()
site_parser.read(os.path.join(settings.BASE_DIR, "init.ini"))

register = template.Library()

@register.filter(name='site_defaults')
def get_setting(my_setting):
  return site_parser.get("settings", my_setting)

2) Load this in a template and call my method, for example: 2)将其加载到模板中并调用我的方法,例如:

{% load static %}
{% load i18n %}
{% load site_defaults %}
<!DOCTYPE html>
<html lang="{% get_setting DEFAULT_LANGUAGE %}">
...

3) Add the module to the INSTALLED_APPS in settings.py 3)将模块添加到settings.py中INSTALLED_APPS

but as the list already includes an entry for vote.initproc and the contained templatetags (like guard) are assumingly loading I thought this was not required. 但由于该列表已经包含了一个vote.initproc的条目, vote.initproc假定正在加载所包含的templatetags(如警卫),我认为这不是必需的。

Restarting the server works, but when I load a page, I get TemplateSyntaxError: 'get_setting'. Did you forget to register or load this tag? 重新启动服务器可以正常工作,但是当我加载页面时,我得到TemplateSyntaxError: 'get_setting'. Did you forget to register or load this tag? TemplateSyntaxError: 'get_setting'. Did you forget to register or load this tag? .

I tried adding my site_defaults to both the installed_apps and middleware, but it throws ModuleNotFoundError: No module named 'site_defaults' 我尝试将site_defaults添加到installed_apps和中间件中,但是会引发ModuleNotFoundError: No module named 'site_defaults'

The Django docs only mention: Django文档仅提及:

The app that contains the custom tags must be in INSTALLED_APPS in order for the {% load %} tag to work. 包含自定义标记的应用程序必须位于INSTALLED_APPS中,才能使{%load%}标记起作用。

but I'm lost on how to add it correctly, so question is: How do I correctly add a custom tag/filter to an existing Django app? 但是我不知道如何正确添加它,所以问题是:如何正确地将自定义标签/过滤器添加到现有的Django应用程序?

You are registering a filter but trying to use it as a tag, and you are registering it under the name "site_defaults" and try to use it as "get_settings". 您正在注册一个过滤器,但尝试将其用作标记,并以“ site_defaults”名称注册它,并尝试将其用作“ get_settings”。 You want simple_tag here, and leave the name alone: 您要在此处使用simple_tag ,并保留名称:

@register.simple_tag
def get_setting(my_setting):
   # ...

https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/#simple-tags https://docs.djangoproject.com/zh-CN/2.0/howto/custom-template-tags/#simple-tags

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

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