当我使用 django.template 中的 Context 时,这里出现了一些困惑的情况。
以下在 python shell 中工作:
>>> from django.template import Context, Template
>>> b=Template('TEST').render(Context())
>>> print b
TEST
当我在单元测试中使用完全相同的代码时,出现以下错误:
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'
单元测试看起来像这样:
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
有谁知道为什么这在 shell 中有效但在单元测试中无效?我感谢每一个提示。
最佳答案
好的,我得到了解决方案:
from decimal import *
是“坏的”这个库也有一个 Context 对象。 感谢任何人阅读!
关于python - 在单元测试中使用 django.template 上下文时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031945/