niwinz / django-jinja

Simple and nonobstructive jinja2 integration with Django.
http://niwinz.github.io/django-jinja/latest/
BSD 3-Clause "New" or "Revised" License
363 stars 102 forks source link

django-rest-framework & django-jinja #134

Closed bryancusatis closed 9 years ago

bryancusatis commented 9 years ago

Hello. I'm using django-jinja for the all of my non-api templates in my current project. I ran into an issue using their TemplateHTMLRenderer with jinja templates. This seems to stem from the context object issue as I get the following error: dictionary update sequence element #0 has length 0; 2 is required.

This stems from backend.py at line 64 where it calls render and passes the wrong type of object to the Jinja2 template (as far as I can tell it is expecting a dict and gets a ContextObject).

I've been trying to look through your code and see if there is anything I can do about this but having some difficulties. I see in base.py that there is a Template that handles the ContextObject correctly. I guess I'm just not seeing why that one isn't being called in this case.

Any thoughts?

Debug Info:

Django Version: 1.8.2
Exception Type: ValueError
Exception Value:    
dictionary update sequence element #0 has length 0; 2 is required
Exception Location: /Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/jinja2/environment.py in render, line 964
Python Executable:  /Users/bryancusatis/Projects/virtualenvs/example-dev/bin/python
Python Version: 2.7.6
Python Path:    
['/Users/bryancusatis/Projects/example',
 '/Users/bryancusatis/Projects/example',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python27.zip',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/plat-darwin',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/plat-mac',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/Extras/lib/python',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/lib-tk',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/lib-old',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages']
Server time:    Thu, 16 Jul 2015 04:31:14 +0000

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/api/old/return_video_set/?video_set=2

Django Version: 1.8.2
Python Version: 2.7.6
Installed Applications:
(u'django.contrib.auth',
 u'django.contrib.contenttypes',
 u'django.contrib.sessions',
 u'django.contrib.messages',
 u'django.contrib.staticfiles',
 u'django.contrib.admin',
 u'tinymce',
 u'rest_framework',
 u'example.core',
 u'example.api',
 'debug_toolbar',
 'django_extensions')
Installed Middleware:
(u'django.contrib.sessions.middleware.SessionMiddleware',
 u'django.middleware.common.CommonMiddleware',
 u'django.middleware.csrf.CsrfViewMiddleware',
 u'django.contrib.auth.middleware.AuthenticationMiddleware',
 u'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 u'django.contrib.messages.middleware.MessageMiddleware',
 u'django.middleware.clickjacking.XFrameOptionsMiddleware',
 u'django.middleware.security.SecurityMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')

Traceback:
File "/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  164.                 response = response.render()
File "/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/django/template/response.py" in render
  158.             self.content = self.rendered_content
File "/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/rest_framework/renderers.py" in render
  167.         return template.render(context)
File "/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/django_jinja/backend.py" in render
  64.         return self.template.render(context)
File "/Users/bryancusatis/Projects/virtualenvs/example-dev/lib/python2.7/site-packages/jinja2/environment.py" in render
  964.         vars = dict(*args, **kwargs)

Exception Type: ValueError at /api/old/return_video_set/
Exception Value: dictionary update sequence element #0 has length 0; 2 is required
niwinz commented 9 years ago

Yes, This is caused because TemplateHtmlRenderer is coupled to the django template engine. Nothing from django jinja can be done.