GET Formset 返回 302 而不是 200 (django)

发布于 2024-12-02 19:22:32 字数 2289 浏览 6 评论 0原文

我已经使用了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2024-12-09 19:22:32

您的登录功能中有一个拼写错误。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文