Django测试不会注意到" django.contrib.auth.context_processors.auth"

时间:2018-05-24 15:41:13

标签: python django templates pytest

我正在为forms.Form类型页面编写基本的django测试。这是表格:

# This is the form to become a seller
class BecomeSellerForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField(min_length=3, max_length=20)
phone_number = forms.IntegerField(max_value=19999999999)

我的测试包含一个基本的帖子请求,而我的响应状态代码是200.但是,我的测试中出现了这个错误:

ValueError: modules context processor require "django.contrib.auth.context_processors.auth"to be in TEMPLATE_CONTEXT_PROCESSORS in your settings file.

它在我的views.py代码中被成功的return语句捕获:

return render(request, 'accounts/become_seller_success.html', context)

令我困惑的是,我100%在我的设置文件中符合此要求:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates', ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.contrib.auth.context_processors.auth',
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.request',
        ],
    },
},
]

那么为什么我的测试会烦恼呢?我一直在环顾四周,但没有看到类似的问题/解决方案。非常感谢帮助

1 个答案:

答案 0 :(得分:0)

我明白了。长话短说 - 当使用RequestFactory时 - 你应该将request.user设置为某个东西。设置为无效。

request.user = None

在我打电话之前添加它会修复所有内容,当我使用它来测试我的权限代码时,它也按预期工作。

感谢小费@Alasdair