django版本:
>>> import django
>>> print django.VERSION
(1, 8, 14, 'final', 0)
python版本:
[root@new_name ~]# python -V
Python 2.7.5
设置中间件
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
url(r'^create_user', views.create_user),
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.contrib.auth import login as auth_login
from django.contrib.auth import logout as auth_logout
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
利用提供的create_user()方法来保存用户名、密码等信息
def create_user(request):
response={}
username = request.GET.get('user_name')
password = request.GET.get('user_pwd')
email = request.GET.get('email')
try:
user = User.objects.create_user(username, email, password)
user.save()
response['state'] = '1'
response['info'] = '创建成功'
except Exception as e:
response['state'] = '0'
response['info'] = '创建失败' + str(e)
return JsonResponse(response)
url(r'^user_login', views.user_login),
def user_login(request):
username = request.POST.get('user_name')
password = request.POST.get('user_pwd')
url_next = request.POST.get('next')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)
request.session['userName'] = usernames
if url_next == "None":
return HttpResponseRedirect('/index')
else:
return HttpResponseRedirect(url_next)
else:
response = "用户名或密码错误"
return render(request, 'login.html', {'msg': response, 'next': 'None'})
url(r'^user_logout', views.user_logout),
def user_logout(request):
auth_logout(request)
return HttpResponseRedirect('/')
参考文献:
http://blog.csdn.net/zzg_550413470/article/details/52239867