django authentication

django session expiry

login and logout view.py

from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render
def user_login(request):
if request.method == "POST":
# try:
# m = models.UserProfile.objects.get(email=request.POST['username'])
# except Exception:
# print("username doesn't exist")
# return render(request, "login.html")
username = request.POST["username"]
password = request.POST["password"]
print(username, password)
user = authenticate(request, email=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# request.session.set_expiry(1800)
return render(request, "index1.html")
else:
return render(request, "login.html")
else:
return render(request, "login.html") def user_logout(request):
logout(request)
return render(request, "login.html")
# try:
# del request.session['member_id']
# except KeyError:
# pass

login index.html

<form method="post" class="m-t" role="form" action="{% url 'login' %}">
{% csrf_token %}
<div class="form-group">
<input placeholder="Username" name="username">
</div>
<div class="form-group">
<input placeholder="Password" name="password">
</div>
<input type="submit" value="login" >
<input type="hidden" name="next" value="{{ next }}" />
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
</form>

logout.html

<a href="{% url 'logout' %}">
<i class="fa fa-sign-out">注销</i>
</a>

urls.py

urlpatterns = [
url(r'^login/', views.user_login, name="login"),
url(r'^logout/', views.user_logout, name="logout"),
]

django authentication的更多相关文章

  1. Django Authentication 用户认证系统

    一. Django的认证系统 Django自带一个用户认证系统,用于处理用户账户.群组.许可和基于cookie的用户会话. 1.1 概览 Django的认证系统包含了身份验证和权限管理两部分.简单地说 ...

  2. Django authentication 使用方法

    转自 : https://docs.djangoproject.com/en/1.8/topics/auth/customizing/

  3. Authentication of Django

    Django Authentication 用户认证系统 一. Django的认证系统 Django自带一个用户认证系统,用于处理用户账户.群组.许可和基于cookie的用户会话. 1.1 概览 Dj ...

  4. Django自定义用户认证系统Customizing authentication

    扩展已有的用户模型Extending the existing User model 有两种方法来扩展默认的User Model而不用重写自己的模型.如果你不需要改变存储在数据库中的字段,而只是需要改 ...

  5. User authentication in Django(用户认证)

    一,概述: auth 系统包括: 1)Users 2)Permissions: Binary (yes/no) flags designating whether a user may perform ...

  6. Django admin定制化,User字段扩展[原创]

    前言 参考上篇博文,我们利用了OneToOneField的方式使用了django自带的user,http://www.cnblogs.com/caseast/p/5909248.html , 但这么用 ...

  7. Redis+Django(Session,Cookie)的用户系统

    一.Django authentication django authentication提供了一个便利的user api接口,无论在py中 request.user,参见Request and re ...

  8. Django 设置cookies与获取cookies.

    在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...

  9. Redis+Django(Session,Cookie、Cache)的用户系统

    转自 http://www.cnblogs.com/BeginMan/p/3890761.html 一.Django authentication django authentication 提供了一 ...

随机推荐

  1. c++常见面试题30道

    1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,new ...

  2. 875. Koko Eating Bananas

    Koko loves to eat bananas.  There are N piles of bananas, the i-th pile has piles[i] bananas.  The g ...

  3. bzoj 5019: [Snoi2017]遗失的答案【dp+FWT】

    满足GL的组合一定包含GL每个质因数最大次幂个最小次幂,并且能做限制这些数不会超过600个 然后质因数最多8个,所以可以状压f[s1][s2]为选s1集合满足最大限制选s2集合满足最小限制 dfs一下 ...

  4. iOS 监测电话呼入

    1.首先引入CoreTelephony框架,代码里: @import CoreTelephony; 项目设置里: 2.定义属性,建立强引用: @property (nonatomic, strong) ...

  5. python操作pymongo

    import pymongo from bson import ObjectId mongo_client = pymongo.MongoClient(host="127.0.0.1&quo ...

  6. 转 PHP scandir() 函数

    实例 列出 images 目录中的文件和目录: <?php $dir = "/images/"; // 以升序排序 - 默认 $a = scandir($dir); // 以 ...

  7. awr 收集时间

    windows 收集 awr 报告,一分钟一个.

  8. C/S WinForm自动升级

    这二天刚好完成一个C/S 自动升级的功能 代码分享一下 /// <summary>    /// 版本检测    /// </summary>    public class ...

  9. nodejs+multiparty 文件上传

    通过表单提交上传文件:     html代码 <form action="/uploadFile" method="post" enctype=" ...

  10. AJPFX关于StringBuffer,StringBuilder类 总结(一)

    StringBuffer,StringBuilder类 StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高 StringBuffer:概述1) ...