Here is the view:
@api_view(['POST'])
@permission_classes([IsAdminUser])
def createProduct(request):
user = request.user
product = Product.objects.create(
user=user,
name='Sample Name',
price=0,
brand='Sample Brand',
countInStock=0,
category='Sample category',
description='',
)
product.save()
serializer = ProductSerializer(product, many=False)
Response(serializer.data)
Here is the urls.py
urlpatterns = [
path('api/products/create/', product_views.createProduct, name='create-product'),
]
Here is the frontend action
export const createProduct = () => async (dispatch, getState) => {
try {
dispatch({ type: PRODUCT_CREATE_REQUEST });
const {
userLogin: { userInfo },
} = getState();
const config = {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${userInfo.token}`,
},
};
const { data } = await axios.post('/api/products/create/',{}, config);
dispatch({
type: PRODUCT_CREATE_SUCCESS,
payload: data,
});
} catch (error) {
dispatch({
type: PRODUCT_CREATE_FAIL,
payload:
error.response && error.response.data.detail
? error.response.data.detail
: error.message,
});
}
};
It's showing "detail": "Method "POST" not allowed." . Seems like everything is fine to me. waiting to hear your opinions.
If I use get method then it shows creating instance error. I tried everything in my knowledge.
So I had the same problem try this - I assume you are have multiple urls . move your create url above the get url.