(已解决):-)(React 和 Django)我无法从我的用户列表中删除用户。 (HTTP 状态码 404)
Posted
技术标签:
【中文标题】(已解决):-)(React 和 Django)我无法从我的用户列表中删除用户。 (HTTP 状态码 404)【英文标题】:(React and Django) I am unable to delete an user from my userlist. (HTTP Status Code 404) 【发布时间】:2021-08-01 13:55:47 【问题描述】:我正在学习一个教程,我试图从用户列表中删除一个用户作为网站的管理员。尽管它也可以从 django 管理中完成,但在本教程中,我们尝试直接在网站本身上执行此操作。它是在页面上完成的,只有管理员才能访问 - http://localhost:3000/admin/userlist
问题是,当我尝试删除某个用户时,(通过单击放置在网页上的按钮(图标))我在控制台中收到此错误 - xhr.js:177 删除 http://localhost:3000/api/users/delete/3/ 404(未找到)
在我的 django 终端中,我收到了这个错误 - 未找到:/api/users/delete/3/ [11/May/2021 09:57:38]“删除 /api/users/delete/3/HTTP/1.1”404 3479
请查看代码的相关部分 - (我已经三次检查了教程中的以下代码,它们似乎是相同的,这就是为什么我非常感谢一些帮助,因为它适用于我的导师,但不适用于我)
user_views.py -
@api_view(['GET'])
@permission_classes([IsAdminUser])
def getUsers(request):
users = User.objects.all()
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
user_urls.py(在 urlpatterns 内)-
path('delete/<str:pk>', views.deleteUser, name='user-delete')
userConstants.js -
export const USER_DELETE_REQUEST = 'USER_DELETE_REQUEST'
export const USER_DELETE_SUCCESS = 'USER_DELETE_SUCCESS'
export const USER_DELETE_FAIL = 'USER_DELETE_FAIL'
userReducers.js -(常量 USER_DELETE_REQUEST、USER_DELETE_SUCCESS 和 USER_DELETE_FAIL 已导入此文件)
export const userDeleteReducer = (state = , action) =>
switch (action.type)
case USER_DELETE_REQUEST:
return loading: true
case USER_DELETE_SUCCESS:
return loading: false, success: true
case USER_DELETE_FAIL:
return loading: false, error: action.payload
default:
return state
userActions.js -(常量 USER_DELETE_REQUEST、USER_DELETE_SUCCESS 和 USER_DELETE_FAIL 已导入此文件)
export const deleteUser = (id) => async (dispatch, getState) =>
try //with the id we know, which user to delete
dispatch(
type: USER_DELETE_REQUEST,
);
const
userLogin: userInfo ,
= getState();
const config =
headers:
"Content-type": "application/json",
Authorization: `Bearer $userInfo.token`
, //only an admin can access this (view), so we need to be authenticated to actually access it
;
const data = await axios.delete(
`/api/users/delete/$id/`,
config
)
dispatch(
type: USER_DELETE_SUCCESS,
payload: data,
);
catch (error)
dispatch(
type: USER_DELETE_FAIL,
payload:
error.response && error.response.data.detail
? error.response.data.detail
: error.message,
);
;
store.js -(相关的是-userDeleteReducer)
import userLoginReducer, userRegisterReducer, userDetailsReducer, userUpdateProfileReducer, userListReducer, userDeleteReducer from "./reducers/userReducers";
and(还在store.js里面)(相关的是userDelete: userDeleteReducer)
const reducer = combineReducers(
productList: productListReducer,
productDetails: productDetailsReducer,
cart: cartReducer,
userLogin: userLoginReducer,
userRegister: userRegisterReducer,
userDetails: userDetailsReducer,
userUpdateProfile: userUpdateProfileReducer,
userList: userListReducer,
userDelete: userDeleteReducer,
orderCreate:orderCreateReducer,
orderCreate: orderCreateReducer,
orderDetails: orderDetailsReducer,
orderPay: orderPayReducer,
orderListMy: orderListMyReducer,
); //stateName: nameOfTheReducer
UserListScreen.js -
import listUsers, deleteUser from "../actions/userActions";
并且(仍在 UserListScreen.js 中)
const userDelete = useSelector((state) => state.userDelete);
const success: successDelete = userDelete; //we are destructuring the userDelete
并且(仍在 UserListSreen.js 中)
useEffect(() =>
if(userInfo && userInfo.isAdmin)
dispatch(listUsers());
else
history.push('/login')
, [dispatch, history, successDelete]); //dependencies
const deleteHandler = (id) =>
if(window.confirm('Are you sure you want to delete this user?'))
dispatch(deleteUser(id))
并且(仍在 UserListSreen.js 中)
<Button
variant="danger"
className="btn-sm"
onClick=() => deleteHandler(user._id)
>
<i className="fas fa-trash"></i>
</Button>
非常感谢您的最终回复。
【问题讨论】:
【参考方案1】:我已经找到了解决这个问题的方法。我的 user_urls.py 文件中缺少正斜杠。 (在 urlpatterns 内)
而不是这个 -
path('delete/<str:pk>', views.deleteUser, name='user-delete')
应该有这个-
path('delete/<str:pk>/', views.deleteUser, name='user-delete')
【讨论】:
以上是关于(已解决):-)(React 和 Django)我无法从我的用户列表中删除用户。 (HTTP 状态码 404)的主要内容,如果未能解决你的问题,请参考以下文章
Django Admin 在生产 Heroku 中引发 500 错误