1
我正在使用基於類的視圖並調用PUT,通過ajax刪除方法,但Django無法識別這些方法並給出403錯誤。但根據this應該支持這些方法基於類的視圖無法識別PUT和DELETE方法
我的代碼:
class MynDetail(TokenRequiredMixin, View):
def post(self, request, *args, **kwargs):
''my code here''
def put(self, request, *args, **kwargs):
''my code here''
def delete(self, request, *args, **kwargs):
''my code here''
的Django沒有認識到把以上觀點刪除
編輯:
我用兩個混入這裏;
class TokenRequiredMixin(object):
"""
check if user token cookie exist or not
"""
def dispatch(self, request, *args, **kwargs):
if 'user_token' not in request.COOKIES.keys():
return redirect(reverse('login-view'))
else:
url = get_base_url(request, 'v1')
valid_token = check_token_validity(url, request.COOKIES.get('user_token'))
if valid_token.get('token') is None:
response = redirect(reverse('login-view'))
response.delete_cookie('user_token')
response.delete_cookie('user_uid')
return response
return super(TokenRequiredMixin, self).dispatch(request, *args, **kwargs)
class OrgValidateMixin(object):
"""
check if user have created an organisation or not
If not, then redirect him to create org page
if yes, then he must not able to create another organisation
so he can't access create organisation page.
"""
def get_token(self):
return self.request.COOKIES.get('user_token')
def get_organisation(self):
token = self.get_token()
url = get_base_url(self.request, 'v1')
return check_user_org(url, token)
def dispatch(self, request, *args, **kwargs):
user_org_exists = self.get_organisation()
current_path = request.get_full_path()
create_org_path = reverse('create-org-view')
if (current_path != create_org_path) and not user_org_exists :
Edit :
return redirect(reverse('create-org-view'))
elif (current_path == create_org_path) and user_org_exists :
return redirect(reverse('portal-dashboard'))
return super(OrgValidateMixin, self).dispatch(request, *args, **kwargs)
HTTP-403表示訪問被禁止。 'TokenRequiredMixin'中是否有一些訪問機制運行? – AKS
同意@AKS,「方法不允許」應該是405而不是403. – piglei
@AKS是它檢查令牌 – user5594493