Django form submit with ajax form.serialize()



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))

登录.html

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

我收到错误:

方法不允许 (开机自检(:/不允许的方法:/[20/10月/2018 04:41:30]"POST/HTTP/1.1" 405 0

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

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

最新更新