1
我已經將頭腦放在了這個位置,差不多3-4天了。如何從Django rest框架的自定義身份驗證類中返回自定義響應對象
讓我來解釋一下情況。我有一個DRF(Django REST框架)基於類的視圖與自定義認證類。據我所知,您可以重寫DRF的BaseAuthentication類的authenticate方法來實現您的自定義身份驗證,而只有在身份驗證失敗的情況下才能提出DRF提供的預定義的例外。
我的問題是,我試圖找到一種方法來返回自定義響應即;將驗證碼HTML直接從認證類發送到前端,以便在我的視圖中實現與認證無關的代碼。
爲了更好地理解我的情況,我在下面提供了一個僞代碼。
class ExampleView(APIView):
authentication_classes = (ExampleCustomAuth,)
def get(self, request):
pass
這是視圖,這部分是絕對好的。
class ExampleCustomAuth(BaseAuthentication):
def authenticate(self, request):
req = request
request = req._request
{
This part of code decides if its required for a
captcha or not
}
if captcha_required:
response = HttpResponse()
response.status_code = 401
response['WWW-Authenticate'] = 'Captcha" id="%s"'% (id)
response.content = loader.render_to_string('captcha.html')
return response # This is where it goes wrong
我相信,它不可能從這裏返回一個響應。
我希望有人找到解決辦法。
預先感謝您!