TemplateView的get方法:对象没有属性'request'

时间:2016-06-02 20:08:51

标签: django django-views django-testing django-tests

将进行多项测试:某些页面的匿名用户将被重定向到登录页面。这就是为什么组织了一些辅助方法的原因。 现在它是TemplateView,然后它将是ListView等。

当我尝试将请求传递给TemplateView子类的get方法时,我收到此错误消息:'HomePageView'对象没有属性'request'。但是TemplateView的get方法的签名是def get(self,request,* args,** kwargs)。

你能帮我一把吗?

/photoarchive/general/views.py

class HomePageView(TemplateView):
    template_name = "general/home.html"

/photoarchive/general/tests.py

class GeneralTest(TestCase):

    def test_anonymous_user_redirected_to_login_page(self, view_instance):
        user = User(username='anonymous', email='vvv@mail.ru', password='ttrrttrr')
        user.is_active = False        
        request = HttpRequest()
        request.user = user
        pdb.set_trace()
        response = view_instance.get(request)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], '/accounts/login/')

    def test_anonymous_user_from_home_page_redirected_to_login_page(self):
        view_instance = HomePageView()
        self.test_anonymous_user_redirected_to_login_page(view_instance)

回溯:

ERROR: test_anonymous_user_from_home_page_redirected_to_login_page (general.tests.GeneralTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 49, in test_anonymous_user_from_home_page_redirected_to_login_page
    self.test_anonymous_user_redirected_to_login_page(view_instance)
  File "/home/michael/workspace/photoarchive/photoarchive/general/tests.py", line 23, in test_anonymous_user_redirected_to_login_page
    response = view_instance.get(request)
  File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py", line 158, in get
    return self.render_to_response(context)
  File "/home/michael/workspace/venvs/photoarchive/lib/python3.5/site-packages/django/views/generic/base.py", line 131, in render_to_response
    request=self.request,
AttributeError: 'HomePageView' object has no attribute 'request'

----------------------------------------------------------------------
Ran 1 test in 0.028s

在pdb断点处进行交互式播放:

-> response = view_instance.get(request)
(Pdb) view_instance
<general.views.HomePageView object at 0x7f9270d550f0>
(Pdb) request
<HttpRequest>
(Pdb) view_instance.get(request)
*** AttributeError: 'HomePageView' object has no attribute 'request'
(Pdb) 

供参考:

class TemplateView(TemplateResponseMixin, ContextMixin, View):
    """
    A view that renders a template.  This view will also pass into the context
    any keyword arguments passed by the URLconf.
    """
    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

1 个答案:

答案 0 :(得分:2)

测试单个CBV方法很棘手。您不打算使用HomePageView()实例化视图。

urls.py中,您可以:

url(r'^$', HomePageView.as_view()),

同样,在您的测试中,您可以执行HomePageView.as_view()。这将返回可以将请求传递给的可调用视图。

view_instance = HomePageView.as_view()
response = view_instance(request)