Django {% include %} Templatetag对{% load %} in文件不起作用



我只是租了一个VPS来尝试发布一个Django项目。在调试模式下,我的pc都运行正常,但当我尝试在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-messages的一个文件: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)

这个问题似乎是指在你的VPS上使用的django-messages应用程序的版本。你使用的是python3.4版本,如果你只使用pip install安装djjango_messages,你将面临旧的Exception语法问题(仅在39行中):

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

它是在主分支https://github.com/arneb/django-messages/commit/659a3dd710051f54e3edc1d76cdfb910d7d04c1a#diff-2006ff4f62d84a3bee25f8b1823d6a5fL39中更改的,所以如果你尝试更新django-messages应用程序版本,你将摆脱SyntaxError问题。

最新更新