我一直在嘗試和搜索,但顯然沒有人遇到這個問題。所以問題是我正在嘗試發送一個 axios 請求以使用正確的 api ur 和密鑰進行洗掉。但是 DRF Model.ViewSet 沒有找到具有適當 PK 的函式。
后端這是我的 view.py 檔案:
class GigList(ViewSet):
def destroy(self, request, pk=None):
print(pk)
Gig.objects.filter(user=request.user, id=request.id).delete()
return Response
網址.py:
router = DefaultRouter()
router.register('api', views.GigList, basename='user')
Frontend 這是axios執行的js檔案:
async deleteGig(id) {
const response = await api.delete(`api/destroy/${id}` `/`,TokenService.getLocalAccessTokenHeader() );
return response
}
Django 輸出:
Not Found: /api/destroy/17/
[09/Jan/2022 23:44:46] "DELETE /api/destroy/17/ HTTP/1.1" 404 8064
如果有人對此有提示或解決方案,我將不勝感激,謝謝大家
uj5u.com熱心網友回復:
如圖所示docs,destroy 端點類似于retrieve/update/partial_update 端點,主要區別在于http 方法。所以它們都使用api/17,并且視圖集根據 http 方法知道要使用哪個動作。
所以改變:
await api.delete(`api/destroy/${id}` `/`,
只是:
await api.delete(`api/${id}` `/`,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/407201.html
標籤:
下一篇:打字稿錯誤:“List<Element>”缺少“ReactElement<any,any>”型別的以下屬性:型別、道具、鍵
