2017-09-21 3 views
0

DRFを使用してregisterおよびsign in API(TokenAuthentication)を作成しようとしています。DRF: "詳細": "メソッド"は使用できません。 "

これは私が次のエラーを取得するサーバーを実行すると、これは

from django.conf.urls import url 
from .views import AuthRegister 
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token 

urlpatterns = [ 
    url(r'^login/', obtain_jwt_token), 
    url(r'^token-refresh/', refresh_jwt_token), 
    url(r'^token-verify/', verify_jwt_token), 
    url(r'^register/$', AuthRegister.as_view()), 
] 

urls.pyである私のviews.py

from rest_framework import status 
from rest_framework.views import APIView 
from rest_framework.response import Response 
from rest_framework.permissions import AllowAny 

from .serializers import AccountSerializer 
from .models import Account 

class AuthRegister(APIView): 
    """ 
    Register a new user. 
    """ 
    serializer_class = AccountSerializer 
    permission_classes = (AllowAny,) 

    def get(self, request, format=None): 
     allquery = Account.objects.all() 
     serializer = AccountSerializer(allquery, many=True) 
     return Response(serializer.data) 

    def post(self, request, format=None): 
     serializer = self.serializer_class(data=request.data) 
     if serializer.is_valid(): 
      serializer.save() 
      return Response(serializer.data, 
        status=status.HTTP_201_CREATED) 
     return Response(serializer.errors, 
     status=status.HTTP_400_BAD_REQUEST) 

class AuthLogin(APIView): 
    ''' Manual implementation of login method ''' 

    def get(self, request, format=None): 
     allquery = Account.objects.all() 
     serializer = AccountSerializer(allquery, many=True) 
     return Response(serializer.data) 

    def post(self, request, format=None): 
     data = request.data 
     email = data.get('email', None) 
     password = data.get('password', None) 

     account = authenticate(email=email, password=password) 
     # Generate token and add it to the response object 
     if account is not None: 
      login(request, account) 
      return Response({ 
       'status': 'Successful', 
       'message': 'You have successfully been logged into your account.' 
      }, status=status.HTTP_200_OK) 

     return Response({ 
      'status': 'Unauthorized', 
      'message': 'Username/password combination invalid.' 
     }, status=status.HTTP_401_UNAUTHORIZED) 

です。

enter image description here

ユーザーがデータベースに追加される詳細を提出した後

が、私がログインするTRときにエラーが発生します。

"Unable to log in with provided credentials."

ないエラーを把握することができ。誰でも助けてください!

答えて

1

あなたのAuthRegisterビューにgetメソッドを実装していません。 DRFはにそのビューのGET要求にを応答する方法を知らないため、そのようなカテゴリの要求を許可しないことを前提としています。

GET要求に対して実行するアクション(サーバーの応答)を実装するgetメソッドを実装する必要があります。

+0

'get()'で何を返すべきですか? –

+0

シリアライザなどのデータhttp://www.django-rest-framework.org/tutorial/3-class-based-views/#rewriting-our-api-using-class-based-views –

+0

' AuthLogin'で、同じget()を実装しました。エラーが発生します。 –

関連する問題