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

Rendering jinja2 template to string #133

Closed Dmitri-Sintsov closed 9 years ago

Dmitri-Sintsov commented 9 years ago
try:
    t = loader.get_template('partial_feed.htm')
    d = template_context_processor(request)
    d['feed'] = feed
    c = Context(d)
    feed_html = t.render(c)
except Exception as e:
    raise e

The line feed_html = t.render(c) raises Traceback: File "/home/user/work/ispdevenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response

  1. response = wrapped_callback(request, _callback_args, *_callback_kwargs) File "/home/user/work/ispdevenv/ispdev/ispdev/decorators.py" in wrapper
  2. result = f(request, _args, *_kwargs) File "/home/user/work/ispdevenv/ispdev/isp_blog/views.py" in feed_add
  3. raise e File "/home/user/work/ispdevenv/ispdev/isp_blog/views.py" in feed_add
  4. feed_html = t.render(c) File "/home/user/work/ispdevenv/lib/python3.4/site-packages/django_jinja/backend.py" in render
  5. return self.template.render(context) File "/home/user/work/ispdevenv/lib/python3.4/site-packages/jinja2/environment.py" in render
  6. vars = dict(_args, *_kwargs)

Exception Type: ValueError at /blog-1/feed-add/ Exception Value: dictionary update sequence element #0 has length 8; 2 is required Request information: GET: No GET data

Dmitri-Sintsov commented 9 years ago

Changed to

t = loader.get_template('partial_feed.htm')
c = RequestContext(request, {'feed': feed}, processors=[template_context_processor])
feed_html = t.render(c)

Now it produces: dictionary update sequence element #0 has length 0; 2 is required

Dmitri-Sintsov commented 9 years ago
t = loader.get_template('partial_feed.htm')
c = RequestContext(request, {'feed': feed})
feed_html = t.render(c)

does not work with the same exception.

Here are parts of settings.py:

TEMPLATES = [ { "BACKEND": "django_jinja.backend.Jinja2", "APP_DIRS": True, "OPTIONS": { "match_extension": ".htm", "app_dirname": "jinja2", 'context_processors': [ 'ispdev.context_processors.template_context_processor' ] } }, { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], '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', ], }, }, ]

django-jinja works fine when rendering to response render(request,...) however it's a AJAX JSON response so I need to render to string.

Dmitri-Sintsov commented 9 years ago
t = loader.get_template('partial_feed.htm')
feed_html = t.render(request=request, context={
    'feed': feed
})

raises no exception but returns empty string. When debugging in django executions of jinja2 script ends at:

    <p>{{ feed.linkify_post()|safe }}</p>

which is strange, because the same partial_feed.htm renders fine with non-AJAX render() shortcut response.

niwinz commented 9 years ago

I'm not clearly understand your problem. But for rendering to string you has this shortcut https://docs.djangoproject.com/en/1.8/topics/templates/#django.template.loader.render_to_string

And django-jinja has tests for it, that successfully pases: https://github.com/niwinz/django-jinja/blob/master/testing/testapp/tests.py#L84

Dmitri-Sintsov commented 9 years ago

Sorry it was my silly mistake. I defined partial_feed.htm as jinja2 macro and obviousely macro does not execute itself when rendering. That's why empty string was returned. Sorry for taking your time.