Django学习笔记

Django学习笔记

安装django

cmd 复制代码
pip install django

创建APP

用django来写后端的时候,要把各个功能分散到各个创建好的APP去实现

在终端输入

cmd 复制代码
python manage.py startapp app01(APP名称)

APP内部文件

admin.py django默认提供了admin后台管理

apps.py app启动类

models.py 对数据库进行操作

tests.py 单元测试

views.py 函数

模板和静态文件

模板

app下面创建templates来放模板

如果setting.py没有

python 复制代码
'DIRS': [BASE_DIR / 'templates']

静态文件会根据app的注册顺序从各个app的templates目录中去找

如果有

会先从项目根目录下面找templates目录,根目录没有就根据app的注册顺序从各个app的templates目录中去找

静态文件

包括image,js,css,plugins

在app下创建static文件夹来存放静态文件

django特有语法

可以在html中

django 复制代码
{% load static %}

引入static文件

html的路径引用可以是

<img src="{% static 'img/1.png' %}">

模板语法

python 复制代码
def tpl(request):
	name="Lee"
	return render(request,'tpl.html',{"n1":name})
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>用户列表</h1>
    <div>{{ n1 }}</div>
</body>
</html>

pymysql

mysql> create database unicom DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

插入数据

mysql 复制代码
create table admin(
	id int not null auto_increment primary key,
	username varchar(16) not null,
	password varchar(64) not null,
	mobile char(11) not null
)default charset=utf8;
python 复制代码
import pymysql
conn=pymysql.connect(host="127.0.0.1",port=3306,user="root",charset="utf8",db='unicom')
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute("insert into admin(username,password,mobile) values('yeke','2131','32423')")
conn.commit()
cursor.close()
conn.close()

使用占位符和元组

python 复制代码
import pymysql
from pymysql import cursors
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", charset="utf8", db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql="insert into admin(username,password,mobile) values(%s,%s,%s)"
cursor.execute(sql,['yeke','1231','21321'])
conn.commit()
cursor.close()
conn.close()

起名

python 复制代码
import pymysql
from pymysql import cursors
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", charset="utf8", db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql="insert into admin(username,password,mobile) values(%(n1)s,%(n2)s,%(n3)s)"
cursor.execute(sql,{"n1":'yeke',"n2":'1231',"n3":'21321'})
conn.commit()
cursor.close()
conn.close()

加输入

python 复制代码
import pymysql
from pymysql import cursors
while True:
    user=input("用户名:")
    passwd=input("密码:")
    mobile=input("手机号:")
    conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", charset="utf8", db='unicom')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    sql="insert into admin(username,password,mobile) values(%s,%s,%s)"
    cursor.execute(sql,[user,passwd,mobile])
    conn.commit()
    cursor.close()
    conn.close()

查询数据

python 复制代码
import pymysql
from pymysql import cursors
## 创建APP
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", charset="utf8", db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql="select * from admin"
cursor.execute(sql)
datalist=cursor.fetchall()
print(datalist)
conn.commit()
cursor.close()
conn.close()

ORM框架

是建立在pymsql,mysqlclient上面的

不需要写sql语句就可以操作数据库

配置mysql

安装mysqlclient

cmd 复制代码
pip install mysqlclient

连接mysql要修改setting.py中数据库的配置

django 复制代码
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "django",
        "USER": "root",
        "PASSWORD": "root",
        "HOST": "127.0.0.1",
        "PORT": 3306,

    }
}

确保配置正确,就可以了

在APP下的models.py中可以创建类

python 复制代码
from django.db import models

# Create your models here.
class UserInfo(models.Model):
    name=models.CharField(max_length=32)
    password=models.CharField(max_length=32)
    age=models.IntegerField()

manage.py

cmd 复制代码
python mange.py makemigrations

python manage.py migrate
相关推荐
wyiyiyi4 小时前
【Web后端】Django、flask及其场景——以构建系统原型为例
前端·数据库·后端·python·django·flask
千层冷面2 天前
Flask ORM 查询详解:Model.query vs db.session.query vs db.session.execute
数据库·python·django·flask
王小王-1232 天前
基于Django的福建省旅游数据分析与可视化系统【城市可换】
数据分析·django·旅游·携程·福建省旅游可视化·旅游数据分析系统·景区数据分析
合作小小程序员小小店2 天前
web网站开发,在线%射击比赛成绩管理%系统开发demo,基于html,css,jquery,python,django,model,orm,mysql数据库
python·mysql·django·jquery·html5
Q_Q19632884753 天前
python基于Hadoop的超市数据分析系统
开发语言·hadoop·spring boot·python·django·flask·node.js
Q_Q5110082853 天前
python的滑雪场雪具租赁服务数据可视化分析系统
spring boot·python·信息可视化·django·flask·node.js·php
noravinsc4 天前
django 如何读取项目根目录下的文件内容
后端·python·django
合作小小程序员小小店4 天前
web安全开发,在线%射击比赛管理%系统开发demo,基于html,css,jquery,python,django,三层mysql数据库
css·mysql·django·html·jquery
桃源学社(接毕设)5 天前
基于Django珠宝购物系统设计与实现(LW+源码+讲解+部署)
人工智能·后端·python·django·毕业设计
alexander0685 天前
Django路由学习笔记
笔记·学习·django