Zartch
Zartch

Reputation: 1025

Django {% include %} Templatetag not working for a {% load %} in file

I just rent a VPS to try to publish a Django project. In debug mode in my pc all runs ok, but when i try to run debug mode in the VPS it comes an error:

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>

In the "usermenu.html" i have this loads that are causing the problem

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

If i load this in "base.html" the {% extends %} tag doesn't work:

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 />

and if i load on rusc.html i still have the SyntaxError but with no html file attached, just the return with the render:

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)

Where i can load this data?

The strange thing is that i installed the same project in Windows and Ubuntu and works fine, this error is only in a Ubuntu VPS from OVH (as far i know).

Any help would be appreciated.

inbox.py is a file of 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)

Upvotes: 1

Views: 1510

Answers (1)

Nick
Nick

Reputation: 1194

It seems that the problem refers to the django-messages app's version that is used on your VPS. You are using python3.4 version and if you will just install django_messages with pip install you'll face an issue with old Exception syntax (just in 39 line):

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

It was changed in the master branch https://github.com/arneb/django-messages/commit/659a3dd710051f54e3edc1d76cdfb910d7d04c1a#diff-2006ff4f62d84a3bee25f8b1823d6a5fL39, so if you try update django-messages app version you will get rid of SyntaxError problem.

Upvotes: 2

Related Questions