Fre*_*ins 2 python iphone django django-csrf ios
我正在开发一个与我的Django应用程序交互的刚刚学习的iOS应用程序.
我正在登录部分:由于csrf保护,我的客户端无法登录Django应用程序.
对于其他视图我只是添加csrf_exempt装饰器来禁用它,但对于内置django.contrib.auth.views.login?
在现代Django中(最后在1.11上测试过),禁用CSRF检查的一种方法是子类化LoginView并覆盖其dispatch方法,该方法明确地用csrf_protect(如此处所示)进行修饰.
由此产生的CBV是:
class DangerousLoginView(LoginView):
'''A LoginView with no CSRF protection.'''
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
if self.redirect_authenticated_user and self.request.user.is_authenticated:
redirect_to = self.get_success_url()
return HttpResponseRedirect(redirect_to)
return super(LoginView, self).dispatch(request, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
这个想法是复制完全相同的方法,同时替换csrf_protect为csrf_exempt.可能有一种更简洁的方法来做到这一点,例如,使用未修饰的.
| 归档时间: |
|
| 查看次数: |
2060 次 |
| 最近记录: |