我使用Django 1.6和Python 3.3。Django - 測試客戶端收到403因爲csrf
我想用django測試客戶端測試POST表單,並在發送請求後收到403。如果我在我的查看方法中添加@csrf_exempt
,則一切正常。 但Django的文件說,它應該啓用CSRF保護工作的優良:
For this reason, Django’s HTTP client for tests has been modified to set a flag on requests which relaxes the middleware and the csrf_protect decorator so that they no longer rejects requests.
這裏是我的測試代碼:
def testSimple(self):
c = Client('Chrome 1.0')
from partners.views import cashier
response = c.post(reverse(cashier.simple), {'x': 123})
self.assertContains(response,'html')
這裏是一個觀點:
def simple(request):
return render(request, 'partners/cashier/index.html')
而且當我運行這個測試時,我看到:
AssertionError: 403 != 200
那麼,你能告訴我我做錯了什麼嗎?
非常感謝你,真的救了我! –