I'm having an issue with a new django 4 app not processing or not allowing POST method. I used to bypass this in Django version 3 by adding a trailing slash "/" to the end of url in Postman API Tester like http://127.0.0.1:8000/api/didipay/ instead of http://127.0.0.1:8000/api/didipay . But now in my Django 4 API I completed the url with the slash but POST method is still not processing the data. It gives me a "500 internal server error" and I don't understand where it is coming from. The GET method is rather giving an empty array which is normal because I've not inserted any data yet in the database. I'm using venv, Django 4 Rest framework, serializer, viewSet and these is my models and API urls configurations:
//Serialize.py
from rest_framework.decorators import permission_classes
from didipay_app.models import Didipay_app
from rest_framework import viewsets, permissions
from .serializers import Didipay_appSerializer
class Didipay_appViewSet(viewsets.ModelViewSet):
queryset = Didipay_app.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = Didipay_appSerializer
// Project folder's urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('didipay_app.urls')),
]
// App folder's urls.py
from rest_framework import routers, urlpatterns
from .api import Didipay_appViewSet
router = routers.DefaultRouter()
router.register('api/didipay', Didipay_appViewSet, 'didipay')
urlpatterns = router.urls
The application's web page says:
HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Vary: Accept
{ "api/didipay": "http://127.0.0.1:8000/api/didipay/" }
And POST is even missing from the allowed method, and I don't understand why, since I granted all permissions in the Serialize.py file (No authentication systhem yet). I will be very greatful for your help regarding this issue. Thank you in advance.
CodePudding user response:
I got it fixed by changing fields = '__ all __' in the Model by all the table attributes in fields = ('id', 'first_name', 'last_name', 'phone', 'email', 'country', 'currency', 'status', 'payingType') because restarting the computer made console erroring out this
TypeError: The
fieldsoption must be a list or tuple or "__ all __"
which was not shown before restarting the system. I also suspect having set fields = '__ all __' instead of fields = '__ all __' (there is an issue about using __ in stackoverflow editor because it is hidden when used like in real code editor, but the overall idea is that there should not be spaces around "all" when using "__".
