I am trying to write a test that checks the HTML returned from the general view of a class. Let's say I have this functional representation that just displays a template:
# views.py from django.shortcuts import render def simple_view(request, template='template.html'): return render(request, template)
At the same time, during testing, I can simply do:
and then check for response . Now, I would like to convert the above into a view class that inherits from TemplateView:
# views.py from django.views.generic import TemplateView class SimpleView(TemplateView): template_name = 'template.html'
Now essentially the same testing method fails:
leads to
Traceback (most recent call last): File "tests.py", line 30, in test_home_page_returns_correct_html response = view_func(request).render() File "lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "lib/python2.7/site-packages/django/views/generic/base.py", line 82, in dispatch if request.method.lower() in self.http_method_names: AttributeError: 'NoneType' object has no attribute 'lower'
I tried setting request.method manually on GET , but this only causes one more error complaining about session not in request .
Is there a way to get a response from a TemplateView with an "empty" request?
source share