2014-12-05 18 views
27
class ChromeLoginView(View): 

    def get(self, request): 
      return JsonResponse({'status': request.user.is_authenticated()}) 

    @method_decorator(csrf_exempt) 
    def post(self, request): 
      username = request.POST['username'] 
      password = request.POST['password'] 
      user = authenticate(username=username, password=password) 
      if user is not None: 
       if user.is_active: 
        login(request, user) 
        return JsonResponse({'status': True}) 
      return JsonResponse({'status': False}) 

Yazının csrf tarafından durdurulduğunu ancak 403 hatası verdiğini umuyorum. @csrf_exempt genel görünüm tabanlı sınıfta çalışmaz

Ama

url(r'^chrome_login/', csrf_exempt(ChromeLoginView.as_view()), name='chrome_login'), 
URLConf

o dekoratör kaldırıp bunu eğer çalışacaktır.

Burada ne oldu? işe yaramadı, çünkü sanırım method_decorator'ın yaptığı şey bu. Kullanıyorum: python3.4 ve django1.7.1

Herhangi bir öneri harika olur.

+0

Django_braces'e bakmalısın ... – rnevius

+0

@rnevius çok teşekkürler, bu mixin lib'i daha önce hiç bilmem. – castiel

+1

Süper harika! Özellikle, bu işi yapmak için görünümünüze [CsrfExemptMixin] (https://django-braces.readthedocs.org/en/v1.4.0/form.html#csrfexemptmixin) ekleyebildiğiniz için. Neredeyse hile yapmak gibi ... – rnevius

cevap

45

Çalıştırmak için csrf_exempt için dispatch yöntemini düzenlemeniz gerekiyor. Bunun anlamı,özniteliğinigörünüm işlevinin kendisinde ayarlamasıdır ve aracı yazılım bunu (en dıştaki) görünüm işlevinde denetler. Yöntemlerden yalnızca birkaçının dekore edilmesi gerekiyorsa,'u dispatch yönteminde kullanmanız gerekir, ancak csrf_protect'u örn. put(). Bir GET, HEAD, OPTIONS veya TRACE HTTP yöntemi kullanılıyorsa, onu dekore edip etmediğiniz kontrol edilmeyecektir.

class ChromeLoginView(View): 
    @method_decorator(csrf_exempt) 
    def dispatch(self, request, *args, **kwargs): 
     return super(ChromeLoginView, self).dispatch(request, *args, **kwargs) 

    def get(self, request): 
     return JsonResponse({'status': request.user.is_authenticated()}) 

    def post(self, request): 
     username = request.POST['username'] 
     password = request.POST['password'] 
     user = authenticate(username=username, password=password) 
     if user is not None: 
      if user.is_active: 
       login(request, user) 
       return JsonResponse({'status': True}) 
     return JsonResponse({'status': False}) 
34

@knbk belirttiğimiz gibi bu dekore edilmelidir dispatch() yöntemdir.

Django 1.9, you can use the method_decorator directly on a class Süresi: Bu dispatch() yöntemi geçersiz önler

@method_decorator(csrf_exempt, name='dispatch') 
class ChromeLoginView(View): 

    def get(self, request): 
     return JsonResponse({'status': request.user.is_authenticated()}) 

    def post(self, request): 
     username = request.POST['username'] 
     password = request.POST['password'] 
     user = authenticate(username=username, password=password) 
     if user is not None: 
      if user.is_active: 
       login(request, user) 
       return JsonResponse({'status': True}) 
     return JsonResponse({'status': False}) 

sadece süslemeleri.

İlgili konular