【问题标题】:Django {% include %} Templatetag not working for a {% load %} in fileDjango {% include %} Templatetag 不适用于文件中的 {% load %}
【发布时间】:2015-10-13 20:02:16
【问题描述】:

我只是租了一个 VPS 来尝试发布一个 Django 项目。在我的电脑的调试模式下一切正常,但是当我尝试在 VPS 中运行调试模式时出现错误:

Django Version:     1.8.5
Exception Type:     SyntaxError
Exception Value:      
invalid syntax (inbox.py, line 39)

Exception Location:     /usr/lib/python3.4/importlib/__init__.py in import_module, line 109
Python Executable:  /usr/bin/python3
Python Version:     3.4.3


Error during template rendering

In template /webapps/rusc/rusc/templates/base.html, error at line 66:

63          </div>
64  
65          <div id="div_user">
66  

      {%  include "usermenu.html" %}



67          </div>
68      </div>

在“usermenu.html”中,我有导致问题的负载

{% load notifications_tags %}
{% load inbox %}

如果我在“base.html”中加载它,{% extends %} 标签将不起作用:

Django Version:     1.8.5
Exception Type:     SyntaxError
Exception Value:    

invalid syntax (inbox.py, line 39)

Exception Location:     /usr/lib/python3.4/importlib/__init__.py in import_module, line 109
Python Executable:  /usr/bin/python3
Python Version:     3.4.3

In template /webapps/rusc/rusc/templates/rusc.html, error at line 1
invalid syntax
1   

      {% extends "base.html" %}



2   
3   
4   {%  block content %}
5       <br />
6       <br />

如果我在 rusc.html 上加载,我仍然有 SyntaxError 但没有附加 html 文件,只是带有渲染的返回:

Environment:


Request Method: GET
Request URL: http://xx.xxx.xx.xx:8000/rusc/

Django Version: 1.8.5
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_filters',
 'django_tables2',
 'django_messages',
 'notifications',
 'registration',
 'autocomplete_light',
 'post',
 'etiqueta',
 'recurs',
 'usuari',
 'buscador',
 'cela',
 'rusc.faq',
 'micawber.contrib.mcdjango')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/webapps/rusc/rusc/views.py" in ruscView
  24.     return render(request,"rusc.html", {'celas':celas,'notifications': notif})
File "/usr/local/lib/python3.4/dist-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in render_to_string
  98.             template = get_template(template_name, using=using)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader.py" in get_template
  35.                 return engine.get_template(template_name, dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/backends/django.py" in get_template
  30.         return Template(self.engine.get_template(template_name, dirs))
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py" in get_template
  167.         template, origin = self.find_template(template_name, dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py" in find_template
  141.                 source, display_name = loader(name, dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/loaders/base.py" in __call__
  13.         return self.load_template(template_name, template_dirs)
File "/usr/local/lib/python3.4/dist-packages/django/template/loaders/base.py" in load_template
  23.             template = Template(source, origin, template_name, self.engine)
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in __init__
  190.         self.nodelist = engine.compile_string(template_string, origin)
File "/usr/local/lib/python3.4/dist-packages/django/template/engine.py" in compile_string
  261.         return parser.parse()
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in parse
  341.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.4/dist-packages/django/template/loader_tags.py" in do_extends
  210.     nodelist = parser.parse()
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in parse
  341.                     compiled_result = compile_func(self, token)
File "/usr/local/lib/python3.4/dist-packages/django/template/defaulttags.py" in load
  1159.                 lib = get_library(taglib)
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in get_library
  1392.             lib = import_library(taglib_module)
File "/usr/local/lib/python3.4/dist-packages/django/template/base.py" in import_library
  1331.         mod = import_module(taglib_module)
File "/usr/lib/python3.4/importlib/__init__.py" in import_module
  109.     return _bootstrap._gcd_import(name[level:], package, level)

Exception Type: SyntaxError at /rusc/
Exception Value: invalid syntax (inbox.py, line 39)

我可以在哪里加载这些数据?

奇怪的是,我在 Windows 和 Ubuntu 中安装了相同的项目并且工作正常,这个错误只出现在 OVH 的 Ubuntu VPS 中(据我所知)。

任何帮助将不胜感激。

inbox.py 是一个 Django 消息文件: https://github.com/arneb/django-messages/blob/master/django_messages/templatetags/inbox.py

from django.template import Library, Node, TemplateSyntaxError

class InboxOutput(Node):
    def __init__(self, varname=None):
        self.varname = varname

    def render(self, context):
        try:
            user = context['user']
            count = user.received_messages.filter(read_at__isnull=True, recipient_deleted_at__isnull=True).count()
        except (KeyError, AttributeError):
            count = ''
        if self.varname is not None:
            context[self.varname] = count
            return ""
        else:
            return "%s" % (count)

def do_print_inbox_count(parser, token):
    """
    A templatetag to show the unread-count for a logged in user.
    Returns the number of unread messages in the user's inbox.
    Usage::

        {% load inbox %}
        {% inbox_count %}

        {# or assign the value to a variable: #}

        {% inbox_count as my_var %}
        {{ my_var }}

    """
    bits = token.contents.split()
    if len(bits) > 1:
        if len(bits) != 3:
            raise TemplateSyntaxError("inbox_count tag takes either no arguments or exactly two arguments")
        if bits[1] != 'as':
            raise TemplateSyntaxError("first argument to inbox_count tag must be 'as'")
        return InboxOutput(bits[2])
    else:
        return InboxOutput()

register = Library()
register.tag('inbox_count', do_print_inbox_count)

【问题讨论】:

  • 您的 inbox.py 文件中存在一些问题。可以附上吗?
  • 如果你在其他系统上部署遇到这个问题,也可能是文件编码(Windows和Linux的行尾码不同)引起的。
  • 我之前在 windows 和 ubuntu 上运行并且工作正常。只是在 VPS 中错误已更新,但对于这个答案,我怀疑 inbox.py 不是问题。 stackoverflow.com/questions/15933773/…
  • 您使用的是什么版本的消息?另外,就像您提到的那样,问题可能在您看来。

标签: python html django django-templates


【解决方案1】:

问题似乎与您的 VPS 上使用的 django-messages 应用程序的版本有关。您使用的是 python3.4 版本,如果您只使用 pip install 安装 django_messages,您将面临旧的异常语法问题(仅在 39 行):

raise TemplateSyntaxError, "first argument to inbox_count tag must be 'as'"

在 master 分支 https://github.com/arneb/django-messages/commit/659a3dd710051f54e3edc1d76cdfb910d7d04c1a#diff-2006ff4f62d84a3bee25f8b1823d6a5fL39 中进行了更改,因此如果您尝试更新 django-messages 应用程序版本,您将摆脱 SyntaxError 问题。

【讨论】:

  • 问题已经解决,现在负载无论如何都不能正常工作。 :) 更进一步。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多