gpt4 book ai didi

Django 身份验证失败,出现 403,没有详细消息

转载 作者:行者123 更新时间:2023-12-02 00:29:53 26 4
gpt4 key购买 nike

我把一些非常简单的东西放在一起。我有一个像这样的小表格:

<form id="loginForm" action>
<input name="email_address" type="text" placeholder="Email">
<input name="password" type="password">
<a id="loginButton">Login</a>
</form>

我的 JavaScript 执行以下操作:

$("#loginButton").click(function(){
$.post("/login/",$("#loginForm").serialize(), function(data) {
console.log("login.");
});
});

我编写了一个自定义的 Django 身份验证后端:

from my.custom.project.models import User

class MyBackend:

def authenticate(self, email_address=None, password=None):
print "Trying to auth"
try:
return User.objects.get(email_address=email_address, password=password)
except User.DoesNotExist:
return None

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

我在我的 settings.py 中配置了这个,如下所示:

AUTHENTICATION_BACKENDS = (
"my.custom.project.auth.MyBackend",
)

在我对 /login/ 路径的实际 View 中,这是我所拥有的:

from django.contrib import auth

def login(request):
print "Trying to auth..."
email_address = request.POST['email_address']
password = request.POST['password']

user = auth.authenticate(email_address=email_address, password=password)

if user != None:
auth.login(request, user)

direct_to_template(request, "my/login/template.json")

这是我的网址映射:

from django.conf.urls.defaults import * 
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

admin.autodiscover()

urlpatterns = staticfiles_urlpatterns

urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^admin/doc', include('django.contrib.admindocs.urls')),
)

urlpatterns += patterns('my.project.views',
(r'^$', 'root'),
(r'^login/?$', 'login'),
(r'^logout/?$', 'logout'),
)

谁能看出我在这里做错了什么?我在 Django 服务器输出中得到的是:

[16/Sep/2011 20:17:47] "POST /login/ HTTP/1.1" 403 2326

我是在吃疯狂的药吗?我忽略了什么?

最佳答案

都是因为Django内置的CSRF系统:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

关于Django 身份验证失败,出现 403,没有详细消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com