Custom template not loading correctly in Django

白昼怎懂夜的黑 提交于 2021-01-27 13:41:38

问题


I'm attempting to sub out the submit_line.html of the DetailView of an admin template in Django, but I'm getting this error:

InvalidTemplateLibrary at /
Module  data_operations.templatetags.data_operations_tags does not have a variable named 'register'

My settings.py contains my app name:

INSTALLED_APPS = [
    ...
    'data_operations',
    ...
]

And template data:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # os.path.join(PROJ_DIR, 'templates')
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'data_operations.apptemplates.load_setting'
            ],
            'libraries':{
                'data_operations_tags': 'data_operations.templatetags.data_operations_tags',

            }
        },
    },
]

Here's data_operations/templatetags/data_operations_tags.py:

from django.contrib.admin.templatetags.admin_modify import submit_row
from django.template.loader import get_template
from django import template

t = get_template('admin/data_operations/submit_line.html')
register = template.Library()
register.inclusion_tag(t, takes_context=True)(submit_row)

I made sure to include an empty __init__.py in the templatetags directory as well.

Finally, I have two templates. data_operations/templates/admin/data_operations/change_form.html:

{% extends "admin/change_form.html" %}
{% load data_operations_tags %}
{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}

And data_operations/templates/admin/data_operations/submit_line.html (which is exactly the same as the default, except I've added a custom 'Cancel' button to the code):

{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_delete_link %}
    {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
    <p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
<input type=button value="Cancel" onClick="javascript:history.go(-1);">
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}
</div>

I don't understand why the error is saying data_operations_tags doesn't have a variable named register. That variable is clearly there in the code. Please help!

来源:https://stackoverflow.com/questions/52449700/custom-template-not-loading-correctly-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!