Django在模态中形成:动态重定向和处理错误

时间:2022-10-24 14:58:45

I am trying to implement a login form inside a modal window using Django. Basically, I am following this tutorial. It works well but I would like to redirect the user to his/her personal page after the form is submitted and validated. Based on the previous guide I think I have to change the code in the highlighted part:

我正在尝试使用Django在模式窗口中实现登录表单。基本上,我正在学习本教程。它运作良好,但我想在提交和验证表单后将用户重定向到他/她的个人页面。基于之前的指南,我想我必须更改突出显示部分中的代码:

$(form).submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: $(this).attr('method'),
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function (xhr, ajaxOptions, thrownError) {
                if ($(xhr).find('.has-error').length > 0) {
                    $(modal).html(xhr);
                    formAjaxSubmit(form, modal);
                } else {
                    $(modal).modal('hide');
                    ###########################
                    REDIRECT HERE TO ACTION URL
                    ###########################
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                // handle response errors here
            }
        });
    });

However, I don't know if this is the correct solution nor if this is possible in this case.

但是,我不知道这是否是正确的解决方案,也不知道在这种情况下是否可行。

1 个解决方案

#1


0  

When you say personal page, that should really be an implicit thing i.e. site.com/dashboard which shows the user currently logged in dashboard. If you want to have redirects for something like a next link (i.e. user attempts to access page needs to log in) or similar, you could always pass the next link back in the response to the xhr request.

当你说个人页面时,这应该是一个隐含的东西,即site.com/dashboard,它显示当前登录仪表板的用户。如果您希望重定向类似下一个链接(即用户尝试访问页面需要登录)或类似内容,您可以始终在响应xhr请求时传回下一个链接。

#1


0  

When you say personal page, that should really be an implicit thing i.e. site.com/dashboard which shows the user currently logged in dashboard. If you want to have redirects for something like a next link (i.e. user attempts to access page needs to log in) or similar, you could always pass the next link back in the response to the xhr request.

当你说个人页面时,这应该是一个隐含的东西,即site.com/dashboard,它显示当前登录仪表板的用户。如果您希望重定向类似下一个链接(即用户尝试访问页面需要登录)或类似内容,您可以始终在响应xhr请求时传回下一个链接。