我的登录API视图出现问题。当我尝试登录时,我得到一个POST 404,但我知道url路径是正确的,因为如果我在视图中放入一个简单的GET请求,它将返回数据。我的代码仍有一些粗略的部分,但我认为这样可行。
login.py
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:prompt="@string/receive_coin_prompt"
android:spinnerMode="dialog" />
serializers.py
from django.contrib.auth import authenticate
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from apiV1.v1.accounts.models.user import User
from apiV1.v1.accounts.serializers.user import UserSerializerLogin
# login
class LoginView(APIView):
authentication_classes = ()
permission_classes = ()
@staticmethod
def post(request):
user = get_object_or_404(User, email=request.data.get('email'))
user = authenticate(username=user.email, password=request.data.get('password'))
if user:
serializer = UserSerializerLogin(user)
return Response(serializer.data)
return Response(status=status.HTTP_400_BAD_REQUEST)
发表
class UserSerializerLogin(UserSerializer):
token = serializers.SerializerMethodField()
@staticmethod
def get_token(user):
"""
Get or create token
"""
token, created = Token.objects.get_or_create(user=user)
return token.key
class Meta:
model = User
fields = ('id', 'email', 'first_name', 'last_name', 'profile', 'role', 'token')