GET Formset 返回 302 而不是 200 (django)
我已经使用了inlineformset,views方法如下:
@login_required
def patron_edit_phone(request, *args, **kwargs):
patron = request.user
PhoneNumberFormSet = inlineformset_factory(Patron, PhoneNumber, extra=1, exclude="kind",can_order=True)
if request.method == "POST":
formset = PhoneNumberFormSet(request.POST, request.FILES, instance=patron)
if formset.is_valid():
formset.save()
messages.success(request, _(u"Votre information de numéro de téléphone a bien été mise à jour"))
else:
formset = PhoneNumberFormSet(instance=patron)
return direct_to_template(request, 'accounts/patron_phone_edit.html', extra_context={'formset': formset, 'patron': patron })
我已经成功测试了inlineformset的“post”方法。这是代码。
def test_patron_phone_edit(self):
self.client.login(username='[email protected]', password='alex')
response = self.client.post(reverse('patron_edit_phone'), {
'phones-TOTAL_FORMS': u'1',
'phones-INITIAL_FORMS': u'',
'phones-MAX_NUM_FORMS': u'',
'phones-0-id' : '1',
'phones-0-patron' : '1',
'phones-0-number' : "11111111",
'phones-0-DELETE' : u''
})
self.assertEquals(response.status_code, 200)
patron = Patron.objects.get(email='[email protected]')
for phone in patron.phones.all():
if phone.id == 1:
self.assertEquals(phone.number, "11111111")
else:
pass
我已经使用 get 方法尝试了以下操作:
def test_patron_phone_get_form(self):
self.client.login(usernamer='[email protected]', password='alex')
response = self.client.get(reverse('patron_edit_phone'))
self.assertEquals(response.status_code, 200)
但这不起作用。我得到的不是 status_code=200,而是 status_code=302。为什么?也许我需要指定 GET 方法的总表单?
非常感谢任何帮助!谢谢!
如果需要更多信息,请发表评论,然后我将粘贴所需的信息。
I have used the inlineformset, the views method is as follows:
@login_required
def patron_edit_phone(request, *args, **kwargs):
patron = request.user
PhoneNumberFormSet = inlineformset_factory(Patron, PhoneNumber, extra=1, exclude="kind",can_order=True)
if request.method == "POST":
formset = PhoneNumberFormSet(request.POST, request.FILES, instance=patron)
if formset.is_valid():
formset.save()
messages.success(request, _(u"Votre information de numéro de téléphone a bien été mise à jour"))
else:
formset = PhoneNumberFormSet(instance=patron)
return direct_to_template(request, 'accounts/patron_phone_edit.html', extra_context={'formset': formset, 'patron': patron })
I have succeeded to test the "post" method for the inlineformset. Here is the code.
def test_patron_phone_edit(self):
self.client.login(username='[email protected]', password='alex')
response = self.client.post(reverse('patron_edit_phone'), {
'phones-TOTAL_FORMS': u'1',
'phones-INITIAL_FORMS': u'',
'phones-MAX_NUM_FORMS': u'',
'phones-0-id' : '1',
'phones-0-patron' : '1',
'phones-0-number' : "11111111",
'phones-0-DELETE' : u''
})
self.assertEquals(response.status_code, 200)
patron = Patron.objects.get(email='[email protected]')
for phone in patron.phones.all():
if phone.id == 1:
self.assertEquals(phone.number, "11111111")
else:
pass
I have tried the following with the get method:
def test_patron_phone_get_form(self):
self.client.login(usernamer='[email protected]', password='alex')
response = self.client.get(reverse('patron_edit_phone'))
self.assertEquals(response.status_code, 200)
But this doesn't work. Instead of getting a status_code=200, I get a status_code=302. Why? Perhaps I need to specify the total forms for the GET method?
Any help is highly appreciated!Thanks!
If more information is needed, leave a comment, then I will paste the needed information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的登录功能中有一个拼写错误。 usernamer 应该是用户名。您将收到 302 重定向,将您引导至登录页面。
You have a typo in the login function. usernamer should be username. You're getting a 302 redirect sending you to the login page.