Questo perché il APIView
ha alcun gestore definito per .put()
metodo così la richiesta in ingresso non può essere associata a un metodo del gestore sulla vista, aumentando così un'eccezione.
(Nota: viewsets.ViewSet
Eredita da ViewSetMixin
e APIView
)
Il metodo dispatch()
nei APIView
controlla se è definito un gestore metodo per la richiesta method
.Se il metodo dispatch()
trova un gestore per il metodo di richiesta, restituisce la risposta appropriata. Altrimenti solleva un'eccezione MethodNotAllowed
.
Come per il codice sorgente di dispatch()
metodo nella classe APIView
:
def dispatch(self, request, *args, **kwargs):
...
...
try:
self.initial(request, *args, **kwargs)
# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
# here handler is fetched for the request method
# `http_method_not_allowed` handler is assigned if no handler was found
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
response = handler(request, *args, **kwargs) # handler is called here
except Exception as exc:
response = self.handle_exception(exc)
self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response
Dal .put()
metodo del gestore non è definito nella vista, DRF chiama il gestore fallback .http_method_not_allowed
. Ciò solleva un'eccezione MethodNotAllowed
.
Il codice sorgente per .http_method_not_allowed()
è:
def http_method_not_allowed(self, request, *args, **kwargs):
"""
If `request.method` does not correspond to a handler method,
determine what kind of exception to raise.
"""
raise exceptions.MethodNotAllowed(request.method) # raise an exception
perché ha funzionato al momento della definizione .put()
nel vostro punto di vista?
Quando è stato definito def put(self, request):
nella propria visualizzazione, DRF può associare il metodo di richiesta in ingresso a un metodo gestore nella vista. Ciò ha comportato il ritorno di una risposta appropriata senza che fosse sollevata un'eccezione.
Quindi, se ho voluto usare creare lo farei basta mapparlo? – floatingpurr
Hai definito un 'router' nei tuoi url quando definisci' update() 'nella tua vista? –
Ho definito un router per registrare viewSet – floatingpurr