Django(7)-项目实战-发布会管理

登录功能

模板页面

sign/templates/index.html

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <title>Login Page</title>
</head>
<body>
  <h1>发布会管理</h1>
  <form action="/login/" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" required><br><br>

    <label for="password">Password:</label>
    <input type="password" id="password" name="password" required><br><br>

    <button id="btn" type="submit">登录</button>
      {% csrf_token %}
  </form>
</body>

</html>

备注:

CSRF verification failed. Request aborted.

表格需要加一个 {% csrf_token %} 的标签。csrf 全称是 Cross Site Request Forgery。这是 Django 提供的防止伪装提交请求的功能。POST 方法提交的表格,必须有此标签。

再次使用POST请求,可以看到客户端提交带有一个token值

登录视图

python 复制代码
from django.http import HttpResponse, Http404
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",'')
        password=request.POST.get("password",'')
        if username=='admin' and password=="admin123":
            return HttpResponse("登录成功")
        else:
            return render(request,'index.html',context={'error':'username or password error!'})

向服务器提交请求时,服务器可以通过request获取用户提交值

如获取POST请求中提交的name为username的值

复制代码
<input type="text" id="username" name="username" required><br><br>
复制代码
request.POST["username"]  

或者 request.POST.get('username','')

获取请求名称:request.method

这里用返回了一个字符串

return HttpResponse("登录成功")

事件管理

模板页面

sign/templatesevent_manage.html

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Event Manage Page</title>
</head>
<body>
<h1>嗨 {{user}} 欢迎</h1>

</body>
</html>

视图函数

python 复制代码
from django.http import HttpResponse, Http404,HttpResponseRedirect
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",'')
        password=request.POST.get("password",'')
        if username=='admin' and password=="admin123":
            response=HttpResponseRedirect('/event_manage/')
            response.set_cookie('user',username,3600)
            return response
        else:
            return render(request,'index.html',context={'error':'username or password error!'})

def event_manage(request):
    username=request.COOKIES.get('user','')
    return render(request,'event_manage.html',{"user":username})

httpresponserediect为重定向,登录后页面被重定向至实践管理页面

这里采用response.set_cookie方法为客户端设置了cookie user=username

使用request.COOKIES.get()方法获取到客户端访问时带的cookie。

如果想给模板传值,需要在render第三个参数提供字典,字段名:值,

在模板中采用双引号{{字段名}}读取返回值。

也可以使用session

复制代码
response.session["user"]=username

要用sesssion需要创建django自带的数据表session

控制台执行python manage.py migrate

重新登陆,可以看到设置的sessionid

使用django认证

创建一个admin用户,这里用的是django自带的管理员账号功能。

运行项目,访问http://127.0.0.1:8000/admin

使用创建的账号登录,并创建一个user

修改登录函数使用django的认证

输入正确账号密码可以登录,输出不存在账号密码登录失败。

设置视图仅登录后可用

设置装饰loginrequired后,直接访问视图会报错

相关推荐
java1234_小锋2 小时前
[免费]基于Python的农产品可视化系统(Django+echarts)【论文+源码+SQL脚本】
python·信息可视化·django·echarts
Danceful_YJ2 小时前
31.注意力评分函数
pytorch·python·深度学习
程序员三藏2 小时前
快速弄懂POM设计模式
自动化测试·软件测试·python·selenium·测试工具·设计模式·职场和发展
循环过三天4 小时前
3.1、Python-列表
python·算法
青青草原羊村懒大王4 小时前
python基础知识三
开发语言·python
傻啦嘿哟4 小时前
Python高效实现Word转HTML:从基础到进阶的全流程方案
人工智能·python·tensorflow
wu_jing_sheng05 小时前
深度学习入门:揭开神经网络的神秘面纱(附PyTorch实战)
python
Ace_31750887765 小时前
淘宝店铺全量商品接口实战:分类穿透采集与增量同步的技术方案
大数据·数据库·python
LeonDL1685 小时前
基于YOLO11深度学习的电动车头盔检测系统【Python源码+Pyqt5界面+数据集+安装使用教程+训练代码】【附下载链接】
人工智能·python·深度学习·pyqt5·yolo数据集·电动车头盔检测系统·yolo11深度学习
wxin_VXbishe7 小时前
springboot在线课堂教学辅助系统-计算机毕业设计源码07741
java·c++·spring boot·python·spring·django·php