gpt4 book ai didi

django - TemplateView的get方法: object has no attribute 'request'

转载 作者:行者123 更新时间:2023-12-02 00:53:38 26 4
gpt4 key购买 nike

会有几个测试:来自某些页面的匿名用户将被重定向到登录页面。这就是组织一些辅助方法的原因。现在是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)

最佳答案

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

在您的 urls.py 中,您可以:

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

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

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

关于django - TemplateView的get方法: object has no attribute 'request' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37601195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com