def login(request): if request.method == 'GET': return render(request, 'login/login.html') if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') code = request.POST.get('captcha') if not username or not password or not code: return res_josn_data.fail_api(msg="用户名或密码没有输入") s_code = request.session.get("code", None) print('验证码:', code, s_code) user_ip = request.META.get('REMOTE_ADDR') print(user_ip) request.session["code"] = None if not all([code, s_code]): # login_log(request, uid=username, is_access=False, desc='验证码错误,请刷新验证码') return res_josn_data.fail_api(msg="验证码错误,请刷新验证码!") if code != s_code: login_log(request, uid=username, is_access=False, desc='验证码错误') return res_josn_data.fail_api(msg="验证码错误") user = User.objects.filter(id_number=username).first() if user is None: login_log(request, uid=username, is_access=False, desc='用户不存在') return res_josn_data.fail_api(msg="用户不存在!") if user.user_status == 0: login_log(request, uid=user.id_number, is_access=False, desc='用户被禁用') return res_josn_data.fail_api(msg="用户被禁用!")
时间: 2023-12-01 16:03:11 浏览: 39
这段代码是一个登录接口的视图函数,处理用户登录时提交的表单数据。如果是 GET 请求,返回登录页面;如果是 POST 请求,则从请求中获取用户名、密码和验证码等数据,进行校验并进行相应的处理。
首先判断用户输入的用户名、密码和验证码是否为空,如果有任何一个为空,则返回错误信息。然后从 session 中获取之前存储的验证码,如果验证码为空,则返回错误信息;如果验证码不为空,则比较用户输入的验证码和 session 中的验证码是否一致,如果不一致,则返回错误信息。
接着根据用户输入的用户名查询数据库,如果用户不存在,则返回错误信息;如果用户存在但是被禁用了,则返回错误信息。最后,如果用户名和密码都正确,且用户状态正常,则表示登录成功,返回成功信息。
相关问题
def home(request): if request.method == 'GET': return render(request, 'login/home.html')
这是一个 Django 视图函数,它会处理 GET 请求,并渲染一个名为 "home.html" 的模板文件并返回给用户。通常情况下,这个函数会在用户访问网站首页时被调用,用于展示网站的主页。如果请求方式不是 GET,那么该函数不会做出任何响应。在模板文件中,你可以通过 Django 模板语言来渲染动态内容,如用户信息、文章列表等等。
from django.shortcuts import render # Create your views here. def index(request): return render(request,'index.html') def login(request): if request.method=='POST': username=request.POST.get('username') userpsd=request.POST.get('password') if username and userpsd: username=username.strip() try: user=Student.objects.get(stuname=username) if user.stupassword==userpsd: return render(request,'index.html') else: message='密码错误' except: message='用户不存在' return render(request,'denglu.html') return render(request,'index2.html') def register(request): if request.method=='POST': username = request.POST.get('username') userpsd = request.POST.get('password') user=Student() user.stuname=username user.stupassword=userpsd user.save() return render(request,'register.html')
这是一个基于 Django 开发的网站的视图函数代码。其中包含三个视图函数:index、login 和 register。
index 视图函数用于渲染首页模板 index.html。
login 视图函数用于处理用户登录的 POST 请求,首先从请求中获取用户名和密码,然后对用户名进行去除空格操作,并通过 Django ORM 根据用户名获取 Student 模型实例,判断用户输入的密码是否与数据库中保存的密码一致,若一致则渲染 index.html 模板,否则返回错误信息并渲染登录页面模板 denglu.html。
register 视图函数用于处理用户注册的 POST 请求,从请求中获取用户名和密码并创建一个 Student 实例,最后保存到数据库中,并渲染注册页面模板 register.html。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)