rom django.shortcuts import render
from programm.models import *
from django.contrib.auth.decorators import login_required
def index(request):
return render(request, 'index.html')
@login_required
def liste(request):
return render(request, 'liste.html', {'lObj': myModel.objects.all()})
在这里直接用 @login_required 来装饰,但是用这种情况的前提是你必须采用 DJANGO的 autho 框架来实现用户管理.
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.
除此之外,还有一些其他的方法:
logout(request[, next_page, template_name, redirect_field_name])
password_change(request[, template_name, post_change_redirect, password_change_form])
password_change_done(request[, template_name])
password_reset(request[, is_admin_site, template_name, email_template_name, password_reset_form,token_generator, post_reset_redirect, from_email])
password_reset_done(request[, template_name])
其用法可以参考如下:
(r'^change_passwd/$', 'django.contrib.auth.views.password_change', {
'template_name': 'change_passwd.html',
'post_change_redirect': '/',
}),
(r'^reset_passwd/$', 'django.contrib.auth.views.password_reset', {
'template_name': 'reset_passwd.html',
'email_template_name': 'reset_passwd_email.html',
'post_reset_redirect': '/reset_done/',
'from_email': 'noreply@jihua.in',
}),
(r'^reset_confirm/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', \
'django.contrib.auth.views.password_reset_confirm', {
'template_name': 'reset_confirm.html',
'post_reset_redirect': '/signin/',
}
),
(r'^reset_done/$', 'django.views.generic.simple.direct_to_template', {'template': 'message.html', 'extra_context': {
'msg': _(u'我们已将一封包含恢复密码步骤的邮件发到了您的邮箱,请查收'),
}}),
本文出自:http://www.yihaomen.com/, 原文地址:http://www.yihaomen.com/article/python/480.htm, 感谢原作者分享。