问题描述
我正在使用带有简单JWT的Django REST框架。我正在尝试创建一个用户注册页面。目前,我收到此错误
禁止(403)CSRF验证失败。请求已中止。
失败原因: CSRF令牌丢失或不正确。
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES' : ('rest_framework.permissions.IsAuthenticated',),'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',
}
我的项目urls文件夹:
urlpatterns = [
path('admin/',admin.site.urls),path("",include("my_api.urls")),path('api-auth/',include('rest_framework.urls')),path('api/token',TokenObtainPairView.as_view()),path('api/token/refresh',TokenRefreshView.as_view()),]
这是我的应用程序的注册端点视图。
def register(request):
if request.method == "POST":
email = request.POST["email"]
# Ensure password matches confirmation
password = request.POST["password"]
confirmation = request.POST["confirmation"]
if password != confirmation:
return render(request,"my_api/register.html",{
"message": "Passwords must match."
})
# Attempt to create new user
try:
# Creates a hashed password.
#password = make_password(password)
user = User.objects.create_user(username=email,email=email,password=password)
user.save()
except IntegrityError as e:
print(e)
return render(request,{
"message": "Email address already taken."
})
login(request,user)
return HttpResponse("Successfully created account.")
else:
return render(request,"my_api/register.html")
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)