【问题标题】:TemplateNotFound after installing django-messages and django-postman安装 django-messages 和 django-postman 后的 TemplateNotFound
【发布时间】:2015-10-06 04:05:54
【问题描述】:

安装 django-postman 和 django-messages 后,我得到了 TemplateNotFound。我显然分别安装了它们——首先是 django-postman,然后是 django-messages。这很简单,但我花了几个小时试图解决这个问题。

我正在使用 Django 1.8,这是使用 pip 进行的全新基础安装。然后我安装了上面的两个包。我的 settings.py 文件的 TEMPLATES 部分如下:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'), 
            #os.path.join(BASE_DIR, 'templates/django_messages'), 
        ],
        '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',
            ],
        },
    },
]

在我的 INSTALLED_APPS 元组中,我还安装了上述软件包。

这是我对 urls.py 的补充:

url(r'^messages/', include('django_messages.urls')),

没有对系统进行其他更改,但是当我转到 /messages 时,我收到以下错误消息:

TemplateDoesNotExist at /messages/inbox/
django_messages/inbox.html
Request Method: GET
Request URL:    http://localhost:8000/messages/inbox/
Django Version: 1.8.3
Exception Type: TemplateDoesNotExist
Exception Value:    
django_messages/inbox.html
Exception Location: /projects/.virtualenvs/blatter/lib/python2.7/site-packages/django/template/loader.py in render_to_string, line 138
Python Executable:  /projects/.virtualenvs/blatter/bin/python
Python Version: 2.7.6

【问题讨论】:

  • 您是否将django-postmandjango-messages 包含在INSTALLED_APPS 中?
  • 是的,让我更新一下我的问题。

标签: django django-messages


【解决方案1】:

问题是因为它从站点的 base.html 扩展而来。邮递员文档中也提到了它:- https://django-postman.readthedocs.org/en/latest/quickstart.html#templates

The postman/base.html template extends a base.html site template, in which some blocks are expected:

    title: in <html><head><title>, at least for a part of the entire title string
    extrahead: in <html><head>, to put some <script> and <link> elements
    content: in <html><body>, to put the page contents
    postman_menu: in <html><body>, to put a navigation menu

可以在这里找到一个可能的解决方案:- django-postman extends a base.html that does not exist

【讨论】:

    【解决方案2】:

    在查看调用的模板并更改扩展/继承参数后,django-messages 的问题已解决。

    被调用的文件 inbox.html 继承了 "django_messages/base.html" ... 运行良好。 “base.html”然后继承自“base.html”,所以这里似乎有一些循环逻辑导致错误。这是默认设置,不是我添加的。当我从“base.html”中删除扩展/继承声明以使其不从自身继承时,django-messages 起作用了。

    也许 Django 1.8 改变了一些带有模板的逻辑?不管怎样,问题都解决了。

    【讨论】:

    • 已确认,能够重现问题。你的解决方案对我有用。
    • 请注意,它并没有尝试继承自身 - base.html 指的是与 django_messages/base.html 不同的模板。
    猜你喜欢
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2018-11-17
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多