4

I got a little confusing situation here when I use the Context from django.template.

The following works in the python shell:

>>> from django.template import Context, Template
>>> b=Template('TEST').render(Context())
>>> print b
TEST

When I use the very same Code in a unittest, I get the follwing Error:

Traceback (most recent call last):
  File "/newsletterapi/tests.py", line 25, in setUp
    b = Template('TEST').render(Context())
  File "/opt/python2.7/lib/python2.7/site-packages/django/template/base.py", line 121, in render
    context.render_context.push()
AttributeError: 'Context' object has no attribute 'render_context'

The unittest looks like this:

from django.test import TestCase
from myproject.newsletterapi.models import Newsletter
from django.utils.termcolors import colorize
from django.db import IntegrityError
from django.template import Template, Context
import random
import datetime
from decimal import *
import string


class NewsletterTest(TestCase):

    def setUp(self):
        b = Template('TEST').render(Context()) # this is line 25
        self.newsletter = Newsletter(body=b)
        self.newsletter.save()

### ... continues here

Does anyone have an idea why this works in the shell but not in the unittest? I appreciate every hint.

Jingo
  • 3,200
  • 22
  • 29

1 Answers1

7

OK, i got the solution:

from decimal import *

is the "bad One" this lib has a Context object too. Thanks for anyone reading!

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
Jingo
  • 3,200
  • 22
  • 29