2014-02-19 51 views
5

我目前正在與REST和Backbone.js的玩弄,跑進這個問題:Django的:類視缺乏屬性COOKIES

'LoginView' object has no attribute 'COOKIES' 

這裏說到下面的代碼可能造成的問題。我已經在javascript中註釋了一些東西,因爲我希望它直接加載到服務器上,但我認爲它將同樣好。

我還可以補充一點,我檢查了表單中的csrf-token並且它在那裏。

views.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/') 

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工作,而我的朋友採取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' 
+0

正如Thomas所回答的那樣,您不能裝飾方法,請檢查django-braces以瞭解如何在基於類的視圖中實現類似的行爲。 – petkostas

+0

會做!最近碰到裝飾者,所以有一些有趣的東西來讀。 –

回答

10

您使用的一個方法的功能裝飾。

這不起作用:裝飾器返回的函數期望其第一個參數是request,並且接收self

用途:

method_decorator(csrf_protect) 
# and so on 

檢查the documentation瞭解更多詳情。

+0

謝謝,它工作得很好,文檔將派上用場。 –

+3

method_decorator的相關導入是from from django.utils.decorators import method_decorator' – EdgeCaseBerg

+0

鏈接被破壞 – pkout