简体   繁体   中英

Django POST method giving 500 internal server error

I am currently working on a Django project. When I am sending a GET request using CURL it is working fine, but in case of POST request it is giving me 500 internal error. I think it has something to do with csrf token and I have no idea what to do with it or how csrf will solve my error.

I have also set DEBUG = False and ALLOWED_HOSTS = ['*'] in settings.py, but then also I am getting the same error:

Server Error (500)

Here is some piece of my code:

urls.py

from django.conf.urls import patterns, url

urlpatterns = patterns(
    'project.views',
    url(r'^users/$', 'userList', name='userList'),
    url(r'^users/(?P<pk>[0-9]+)$', 'userDetail', name='userDetail'),
)

This is a function from views,py

@api_view(['GET', 'POST'])

def userList(request):
    if request.method == 'GET':
        user = users.objects.all()
        serializer = UsersSerializer(user, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = UsersSerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(
            serializer.errors, status=status.HTTP_400_BAD_REQUEST)

This is the command I am using in the terminal:

curl -X POST http://127.0.0.1:8000/users/ -d '{"username":"root", "email":"abc@gmail.com"}' -H "Content-Type: application/json"

Django version number : 1.9

In POSTMAN I am getting the error as:

{ "username": [ "This field is required." ], "email": [ "This field is required." ] }

应该不是data=request.data (小写的data )吗?

实际上应该不应该像这样分配数据来发布?

serializer = UsersSerializer(data=request.POST)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM