[英]Django. TestCase login_required
This is my code: 这是我的代码:
from django.test import TestCase
from django.contrib.auth.models import User
class BetsTest(TestCase):
def setUp(self):
user = User.objects.create_user('test', 'test', 'test')
user.save()
self.client.login(username='test', password='test')
def test_details(self):
response = self.client.post('/bets/makebet/5', {'points' : '24'})
self.failUnlessEqual(response.status_code, 200)
And my view has decorator @login_required.. And no matter what I am doing my response is status: 301.... 而且我的视图具有装饰器@login_required。无论我在做什么,我的响应都是状态:301。
Well, if you're posting something, it's probably gonna redirect you to a success page right? 好吧,如果您要发布一些内容,它可能会将您重定向到成功页面,对吗? That's not a 200 code. 那不是200码。
Set follow=True
on your post
call and it should work fine. 在您的post
通话中设置follow=True
,它应该可以正常工作。
response = self.client.post('/bets/makebet/5', {'points' : '24'}, follow=True)
You can also access the redirects if you do this with response.redirect_chain
如果您使用response.redirect_chain
重定向,也可以访问重定向
https://docs.djangoproject.com/en/dev/topics/testing/tools/ https://docs.djangoproject.com/en/dev/topics/testing/tools/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.