django用户认证-创新互联
利用django自带认证功能实现用户登录认证。
建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。创新互联专业提供成都做网站、成都网站建设,网页设计,网站制作(企业站、响应式网站建设、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!views.py
# Create your views here. from django.shortcuts import render_to_response,render,get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.template.context import RequestContext from django.contrib.auth.models import User from django.contrib import auth from forms import LoginForm def login(request): if request.method == 'GET': form = LoginForm() return render_to_response('login.html',RequestContext(request,{'form':form,})) else: form = LoginForm(request.POST) if form.is_valid(): username = request.POST.get('username','') password = request.POST.get('password','') user = auth.authenticate(username=username,password=password) if user is not None and user.is_active: auth.login(request,user) return render_to_response('index.html',RequestContext(request)) else: return render_to_response('login.html',RequestContext(request,{'form':form,'password_is_wrong':True})) else: return render_to_response('login.html',RequestContext(request,{'form':form,})) @login_required def logout(request): auth.logout(request) return HttpResponseRedirect("/login/") @login_required def index(request): return render_to_response('index.html')
froms.py
#coding=utf-8 from django import forms from django.contrib.auth.models import User class LoginForm(forms.Form): username = forms.CharField( required = True, label="用户名", error_messages={'required':'请输入用户名'}, widget=forms.TextInput( attrs={ 'placeholder': "用户名", 'class':'form-control' } ) ) password = forms.CharField( required=True, label="密码", error_messages={'required':'请输入密码'}, widget=forms.PasswordInput( attrs={ 'placeholder':"密码", 'class':'form-control' } ), ) def clean(self): if not self.is_valid(): raise forms.ValidationError("用户名和密码为必填项") else: cleaned_data = super(LoginForm,self).clean()
login.html
登录 {% if password_is_wrong %}{% endif %}错误!
用户名或密码错误用户登录
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享文章:django用户认证-创新互联
链接分享:http://hbruida.cn/article/iigid.html