我正在尝试POST到Django Rest Framework API View页面。服务器在localhost上运行,DEBUG = True。我收到500错误,但它立即重定向到GET同一页面,没有给我提供有用的错误调试信息。
[16/Jan/2016 19:07:00] "POST /account/create/ HTTP/1.1" 500 12031
[16/Jan/2016 19:07:00] "GET /account/create/ HTTP/1.1" 405 4810
因此,我没有获得有关修复500错误的任何有用信息,而是获得了一个页面“不允许使用方法GET”。无论如何要解决这个问题?
以下是代码:
class CreateAccount(APIView):
def post(self, request, format=None):
serializer = RegistrationSerializer(data=request.data)
if serializer.is_valid():
try:
print Account.objects.get(username="reparadocs")
account = Account.objects.get(username=serializer.data['username'])
except ObjectDoesNotExist:
account = Account.objects.create_user(
serializer.data['username'],
'',
serializer.data['password']
)
token = Token.objects.create(user=account)
return Response({'token': token[0].key}, status=status.HTTP_201_CREATED)
return Response({'errors': 'Username already exists'}, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)