本篇内容介绍了“Python Django怎么实现登录注册”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
成都创新互联专注骨干网络服务器租用十载,服务更有保障!服务器租用,成都移动云计算中心 成都服务器租用,成都服务器托管,骨干网络带宽,享受低延迟,高速访问。灵活、实现低成本的共享或公网数据中心高速带宽的专属高性能服务器。点击(此处)折叠或打开
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.encoding import python_2_unicode_compatible
###普通用户的数据模型
@python_2_unicode_compatible
class NewUser(AbstractUser):
profile = models.CharField('profile', default='', max_length=256)
def __str__(self):
return self.username
# Create your models here.
##admin
# -*- coding: utf-8 -*-
from django.contrib import admin from .models import NewUser class NewUserAdmin(admin.ModelAdmin): list_display = ('username','date_joined', 'profile') admin.site.register(NewUser,NewUserAdmin)
AUTH_USER_MODEL = "pric.NewUser"
# -*- coding: utf-8 -*-
from django.shortcuts import render
from .forms import *
from .models import *
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponse
from django.shortcuts import render, redirect,render_to_response
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
return render_to_response('index.html', locals())
###登录
def log_in(request):
if request.method == 'GET':
form = LoginForm()
return render(request, 'login.html', {'form': form})
if request.method == 'POST': # 当提交表单时
form = LoginForm(request.POST) # form 包含提交的数据
if form.is_valid(): # 如果提交的数据合法
username = form.cleaned_data['uid']
password = form.cleaned_data['pwd']
user = authenticate(username=username, password=password)
if user is not None: ###如果用户验证存在,
login(request, user) #开始登录
return redirect('index') #跳转首页
else:
return render(request, 'login.html', {'form': form, 'msg': "password or username is not ture!"})
else: ###如果提交的数据不合法,继续登录
return render(request, 'login.html', {'form': form})
###登出
@login_required
def log_out(request):
logout(request)
return redirect('login')
# Create your views here.
###注册
def register(request):
error1 = "this name is already exist"
valid = "this name is valid"
if request.method == 'GET':
form = RegisterForm()
return render(request, 'register.html', {'form': form})
if request.method == 'POST': ###提交
form = RegisterForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
email = form.cleaned_data['email']
password1 = form.cleaned_data['password1']
password2 = form.cleaned_data['password2']
if password1 != password2:
return render(request, 'register.html', {'form': form, 'msg': "two password is not equal"})
else:
NewUser.objects.create_user(username=username,password=password1,email=email)
#user = NewUser(username=username, email=email, password=password1)
#user.save()
# return render(request, 'login.html', {'success': "you have successfully registered!"})
return redirect('login')
else:
return render(request, 'register.html', {'form': form})
方式一:
前端request.user.is_authenticated()判断当前用户是否登录
Welcome!>
{% if request.user.is_authenticated %}
hihi: {{ request.user }}
{% endif %}
方式二 将request.user.is_authenticated判断写入views视图再传入html中
@login_required
def index(request):
if request.user.is_authenticated(): # 判断用户是否已登录
user = request.user;
return render_to_response('index.html', locals())
Welcome!>
欢迎登录!{{ user }}
“Python Django怎么实现登录注册”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!