Fixed Django 2.1 compatibility due to removal of django.contrib.auth.login()/logout() views. (#5510)

This commit is contained in:
Mariusz Felisiak 2017-10-18 09:46:27 +02:00 committed by Carlton Gibson
parent 320f10ad00
commit 1a526c153e
2 changed files with 13 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import inspect
import django import django
from django.apps import apps from django.apps import apps
from django.conf import settings from django.conf import settings
from django.contrib.auth import views
from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import \ from django.core.validators import \
MaxLengthValidator as DjangoMaxLengthValidator MaxLengthValidator as DjangoMaxLengthValidator
@ -332,3 +333,12 @@ def authenticate(request=None, **credentials):
return authenticate(**credentials) return authenticate(**credentials)
else: else:
return authenticate(request=request, **credentials) return authenticate(request=request, **credentials)
if django.VERSION < (1, 11):
login = views.login
login_kwargs = {'template_name': 'rest_framework/login.html'}
logout = views.logout
else:
login = views.LoginView.as_view(template_name='rest_framework/login.html')
login_kwargs = {}
logout = views.LogoutView.as_view()

View File

@ -15,12 +15,11 @@ and you should make sure your authentication settings include `SessionAuthentica
from __future__ import unicode_literals from __future__ import unicode_literals
from django.conf.urls import url from django.conf.urls import url
from django.contrib.auth import views
template_name = {'template_name': 'rest_framework/login.html'} from rest_framework.compat import login, login_kwargs, logout
app_name = 'rest_framework' app_name = 'rest_framework'
urlpatterns = [ urlpatterns = [
url(r'^login/$', views.login, template_name, name='login'), url(r'^login/$', login, login_kwargs, name='login'),
url(r'^logout/$', views.logout, template_name, name='logout'), url(r'^logout/$', logout, name='logout'),
] ]