【问题标题】:Django form submit with ajax form.serialize()使用 ajax form.serialize() 提交 Django 表单
【发布时间】:2019-03-24 22:41:25
【问题描述】:

views.py

def login(request):
        password = request.POST.get('password')
        mobile = request.POST.get('mobile')
        user = authenticate(username=mobile, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect("/owner/?own=" + str(user.id))

login.html

$('.login-form').on('submit', function(event) {
        event.preventDefault();
        var form = $(this);
        $.ajax({
            url: '/ajax/login/',
            type: "POST",
            data: form.serialize()
          success: function(data) {
 });
});

我遇到了错误:

不允许的方法(POST):/ 不允许的方法:/ [20/Oct/2018 04:41:30]“POST / HTTP/1.1”405 0

【问题讨论】:

  • 你能从你的urls.py 为你的login 函数发布配置吗?
  • 当我手动发送帖子数据时,此代码有效。但使用 form.serialize() 它不起作用。

标签: python django python-3.x django-forms django-views


【解决方案1】:

您的登录函数和 django 内置登录函数之间存在名称冲突,请将您的函数重命名为 user_login 之类的其他名称。

【讨论】:

    【解决方案2】:

    我想你缺少 csrf 令牌?尝试将 Django csrf 令牌传递到您的 JQuery。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2017-07-26
    • 2021-03-19
    • 1970-01-01
    • 2011-03-13
    • 2015-12-21
    • 2020-11-24
    • 2016-08-20
    • 2018-07-22
    • 2018-09-17
    相关资源
    最近更新 更多