2017-06-01 6 views
0

私は適切な方法でDjangoセッションを使用しているかわかりません。これは私が使用することができますどのように良い方法であれば、私はif request.user.is_authenticated()@login_required を使用した認証が必要なすべての方法では適切なDjangoセッションテンプレートはどのように見えるでしょうか?

from django.contrib.auth import authenticate, login 

@api_view(['POST']) 
def my_login(request): 
    if request.method == 'POST': 
    username = request.POST.get('username', '') 
    password = request.POST.get('password', '') 
    user = authenticate(username=username, password=password) 
    if user is not None: 
     if user.is_active: 
     request.session.set_expiry(86400)    
     login(request, user) 

url(r'^login/$', views.my_login):私はこのビューとURLが(?それはOKです)を使用してログインするには@api_view(['GET', 'POST'])@login_require

@login_require 
@api_view(['GET', 'POST']) 
def my_func(request): 
    #... 

ご協力いただきありがとうございます。私は、それは良い解決策ではないようです。

答えて

0

django-rest-frameworkを使用しているので、デフォルトの認証方法を使用することをお勧めします。

使用@authentication_classes((SessionAuthentication、BasicAuthentication))

drf setting-the-authentication-scheme

関連する問題