You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

106 lines
3.8 KiB

from django.shortcuts import render, redirect
from django.http.response import HttpResponse
from django.urls import reverse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required, permission_required
from user_manager.models import MyUser
# Create your views here.
def user_reg(request):
if request.method == 'GET':
return render(request, 'user_manager/user_reg.html')
if request.method == 'POST':
user_name = request.POST.get('username')
user_pw = request.POST.get('password')
if User.objects.filter(username=user_name):
info_msg = '用户已经存在'
else:
user_data = {'username': user_name, 'password': user_pw, 'email': request.POST.get('email'), 'is_staff': 1,
'is_active': 1, 'is_superuser': 1}
User.objects.create_user(**user_data)
info_msg = '添加用户成功'
return render(request, 'user_manager/user_reg.html', {'info': info_msg})
def user_login(request):
if request.method == 'GET':
return render(request, 'user_manager/login.html')
if request.method == 'POST':
user_name = request.POST.get('username')
user_pw = request.POST.get('password')
if User.objects.filter(username=user_name):
user = authenticate(username=user_name, password=user_pw)
print(user)
if user:
print(user.is_active)
if user.is_active:
login(request, user)
info = '登录成功'
else:
info = '用户未激活'
else:
info = '账号密码错误'
else:
info = '账号不存在'
return render(request, 'user_manager/login.html', {'info': info})
def myuser_reg(request):
if request.method == 'GET':
return render(request, 'user_manager/user_reg.html')
if request.method == 'POST':
uname = request.POST.get('username')
pwd = request.POST.get('password')
if MyUser.objects.filter(username=uname):
info = '用户已存在'
else:
d = dict(username=uname, password=pwd, email=request.POST.get('email'), is_staff=1, is_active=1,
is_superuser=1, photo='', weChat='coder', level='1')
user = MyUser.objects.create_user(**d)
info = '注册成功'
return render(request, 'user_manager/user_reg.html', {'info': info})
def myuser_login(request):
if request.method == 'GET':
return render(request, 'user_manager/login.html')
if request.method == 'POST':
uname = request.POST.get('username')
pwd = request.POST.get('password')
if MyUser.objects.filter(username=uname):
user = authenticate(username=uname, password=pwd)
if user:
if user.is_active:
login(request, user)
info = '登录成功'
else:
info = '用户未激活'
else:
info = '账号密码错误'
else:
info = '用户不存在'
return render(request, 'user_manager/login.html', {'info': info})
@permission_required('user_manager.change_myuser')
@login_required
def myuser_edit(request):
return render(request, 'user_manager/edit.html')
def myuser_logout(request):
logout(request)
return redirect(reverse('user_manager_login'))
@permission_required('user_manager.view_myuser')
@login_required
def user_index(request):
users = MyUser.objects.all()
return render(request, 'user_manager/user_index.html', {'users': users})
def test_middle(request):
return HttpResponse('执行了')