一、需要知道的新知识点
1、刷新验证码,给src属性加一个?号。加个?会重新去请求
1 2 3 4 5 6 7 8 |
# 给验证码刷新 $(".vialdCode_img").click(function () { 方式一:dom方法 $(this)[0].src+="?"#} 方式二:jQuery的attr方法 $(this).attr("src",$(this).attr("src")+'?') }) })
|
2、当登录成功跳转,或者注册成功跳转
1 2 3 |
$(".register").click(function () { loction.href = '/register/' });
|
3、超时后消失
1 2 3 4 5 6 |
setTimeout(foo, 3000) function foo() { $(".error").html("") }
|
4、auth模块的使用
模块的导入:
1 |
from django.contrib import auth
|
几个使用方法:
1 、authenticate() :验证用户输入的用户名和密码是否相同
提供了用户认证,即验证用户名以及密码是否正确,一般需要username password两个关键字参数
1 |
user = auth.authenticate(username=username, password=password)
|
2 、login(HttpRequest, user):登录
该函数接受一个HttpRequest对象,以及一个认证了的User对象
此函数使用django的session框架给某个已认证的用户附加上session id等信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def login(request): if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") print(username, password) user = auth.authenticate(username=username, password=password) # 验证用户名和密码 if user is not None and user.is_active: # 如果认证成功,就让登录 auth.login(request, user) request.session['user'] = username # 将session信息记录到浏览器 response = HttpResponseRedirect("/books/") return response elif user is None: return render(request, "index.html", {"s1": "用户名不存在!"}) else: s = "用户名或密码错误" return render(request, "index.html", {"s": s}) return render(request, "index.html")
|
3 、logout(request) 注销用户
该函数接受一个HttpRequest对象,无返回值。当调用该函数时,当前请求的session信息会全部清除。该用户即使没有登录,使用该函数也不会报错。