当前位置:网站首页>Django4.0 + Web + MySQL5.7 实现简单登录操作
Django4.0 + Web + MySQL5.7 实现简单登录操作
2022-07-25 08:42:00 【Ding Jiaxiong】
Django
文章目录
Django + Web + MySQL5.7 实现简单登录操作
1.1 Django + Web + MySQL5.7
1.1.1 创建项目

1.1.2 创建登录应用(Django常称App)
python manage.py startapp login

1.1.3 注册App

1.1.4 配置数据库
注意:Django不能创建数据库,需要手动提前创建好

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'login',
'USER': 'root',
'PASSWORD': '200039',
'HOST': '127.0.0.1',
'PORT': 3306,
}
}

1.1.5 创建视图及配置urls


在templates目录下新建login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="/login/">
{% csrf_token %}
<input type="text" name="user" placeholder="用户名">
<input type="password" name="pwd" placeholder="密码">
<button type="submit" value="提交">提交</button>
{
{ error_msg }}
</form>
</body>
</html>
1.1.6 启动服务访问测试一下

或者在命令行中执行一下命令
python manage.py runserver


1.1.7 创建模型(实体类)

命令行中执行命令
python manage.py makemigrations
python manage.py migrate

查看数据库

手动插入一条数据(ORM也可以,但不是这次示例的重点,好吧,这个示例没有重点)

1.1.8 编写处理逻辑views.py

def login(request):
if request.method == "GET":
return render(request,"login.html")
username = request.POST.get("user")
password = request.POST.get("pwd")
user = User.objects.get(id = 1)
user_name = user.username
pass_word = user.password
if username == user_name and password == pass_word:
return HttpResponse("登录成功")
return render(request,"login.html",{
"error_msg":"登录失败"})
1.1.9 启动服务,测试

点击提交

修改信息

提交

边栏推荐
- Wechat reservation applet graduation design of applet completion works (3) background function
- C语言基础
- 聊下自己转型测试开发的历程
- Chapter 3 business function development (query clues)
- [ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
- JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
- PHP gets all child nodes under any parent node of the tree structure
- SSM+JSP+MYSQL实现的宠物领养收养管理系统源码
- IP command usage details
- Wechat sports ground reservation applet graduation project of applet completion works (1) development outline
猜你喜欢

【黑马程序员】Redis学习笔记003:Redis事务

@Autowired注解的实现原理

Wechat sports ground reservation applet graduation design of applet completion works (3) background function

Data warehouse ODS, DWD floor, 220616, HM,

Redis学习

Wechat applet ordering system graduation design of applet completion works (2) applet function

When testing VPN, the IP found by the command line is inconsistent with that of Baidu search

@Use of data annotation (instead of get and set methods in entity classes)

【黑马程序员】Redis学习笔记004:主从复制+哨兵模式+集群

Some easy-to-use plug-ins and settings installed in vscode
随机推荐
【黑马程序员】Redis学习笔记005:企业级解决方案
Practice of establishing dynamic programming state transition equation
The international summit osdi included Taobao system papers for the first time, and end cloud collaborative intelligence was recommended by the keynote speech of the conference
华为设备远程登录(Telnet、SSH)配置
[Sesame Street family] & Bert Bart Roberta
Implementation of depth first and breadth first traversal of binary tree
Message Oriented Middleware
Hash table questions (Part 1)
Redis fragment cluster
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
Talk about your transformation test development process
Wechat reservation of small program completion works (5) assignment book of small program graduation project
C#入门系列(三十) -- 异常处理
Redis4.0.14 sentinel automatic failover failed
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
Online bookstore system based on jsp+servlet+mysql
OpenGL es to achieve the effect of "big head, small head" and "head shaking"
Hotel room management system based on jsp+servlet+mysql
Data warehouse ODS, DWD floor, 220616, HM,
Wechat sports ground reservation applet graduation project of applet completion works (1) development outline