gpt4 book ai didi

python - Django:缺少属性 COOKIES 的类 View

转载 作者:太空狗 更新时间:2023-10-29 21:31:46 25 4
gpt4 key购买 nike

我目前正在使用 REST 和 backbone.js 并遇到了这个问题:

'LoginView' object has no attribute 'COOKIES'

以下代码可能导致了问题。我已经在 javascript 中注释掉了一些东西,因为我希望它直接加载到服务器,但我认为将它带在身边也一样好。

我还可以补充一点,我已经检查了表单中的 csrf-token,它就在那里。

View .py

class LoginView(TemplateView):
authentication_form=LoginForm
form_class = LoginForm
redirect_field_name=REDIRECT_FIELD_NAME
template_name = 'front/login.html'
initial = {'key': 'value'}

def get(self, request, *args, **kwargs):
form = self.form_class(initial=self.initial)
return render(request, self.template_name, {'form': form})

@method_decorator(sensitive_post_parameters())
@csrf_protect
@never_cache
@api_view(['GET', 'POST'])
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
print request.COOKIES('csrftoken')
print request.POST.get('csrfmiddlewaretoken')
if form.is_valid():
#if not is_safe_url(url=redirect_to, host=request.get_host()):
# redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)
print request.POST.get('email')
#user = self.get_user(request.POST.get('email'))
#print user
#auth = UserDataAuthentication(request, user)
user = authenticate(email=request.POST.get('email'), password=request.POST.get('password'))
if user is not None:
auth_login(request, user)
return Response(user, status=status.HTTP_201_CREATED)
return HttpResponseRedirect('/login/')

登录.js

var csrftoken = $('meta[name="csrf-token"]').attr('content');
SigninView = Backbone.View.extend({
events: {
//"click button[type=submit]": "sendRequest"
},
sendRequest: function( event ){
//event.preventDefault();
var csrftoken = $.cookie('csrftoken');
var url = '/login/validate/';
var email = $("#id_email").val();
var password = $("#id_password").val();
var items = {
email: email,
password: password,
csrfmiddlewaretoken: csrftoken
};
console.log(csrftoken);
$.ajax({
url:url,
type:'POST',
dataType:"json",
data: items,
success: function (data) {
var json = $.parseJSON(data);
console.log(data);
},
error: function (xhr, textStatus, error) {
$("#form_error").css('padding','7px').css('border-radius','4px').html('Error recieved: ' + error).fadeIn();
console.log("Status: "+textStatus);
console.log("Type: "+error);
}
});
}
});
var signin_view = new SigninView({
el: $("#login_form")
});

我们是两个人在做这件事,我自己负责使用 Python,而我的 friend 负责 js 部分。我还没有足够的 Django 经验来真正找到可能导致 cookie 出现错误的原因。大多数时候,如果可以的话,我尽量远离 cookie,但这似乎很难。

当然还有:回溯:

Traceback:
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
75. return view(request, *args, **kwargs)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
95. result = middleware.process_view(request, view_func, args, kwargs)
File "/home/ryuu/Programming/Python/tabr/venv/local/lib/python2.7/site-packages/django/middleware/csrf.py" in process_view
111. request.COOKIES[settings.CSRF_COOKIE_NAME])

Exception Type: AttributeError at /login/validate/
Exception Value: 'LoginView' object has no attribute 'COOKIES'

最佳答案

您在方法上使用了函数装饰器。

这是行不通的:装饰器返回的函数期望它的第一个参数是一个request,并接收到self

使用:

from django.utils.decorators import method_decorator

csrf_protected_method = method_decorator(csrf_protect)
# and so on

检查 the documentation了解更多详情。

关于python - Django:缺少属性 COOKIES 的类 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880905/

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