Django自带auth_user表操作:
views.py
from django.contrib import auth#引入auth模块
from django.contrib.auth.models import User # auth应用中引入User类
(1)用户注册,添加记录(create_use普通用户,create_superuser超级用户)
# 注册
def register(request):
if request.method == 'GET':
register_obj = RegisterForm()
return render(request, 'register.html', { 'register_obj': register_obj})
elif request.method == 'POST':
data = request.POST
register_obj = RegisterForm(data)
if register_obj.is_valid():
user_obj = register_obj.cleaned_data
print(user_obj)
username = user_obj.get('name')
password = user_obj.get('password')
#通过auth模块,进行新用户注册,使用User表创建新用户(create_use普通用户,create_superuser超级用户)
if not User.objects.filter(username=username).exists():
new_obj = User.objects.create_user(username=username, <