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
相关推荐
Q_Q5110082853 小时前
python+uniapp基于微信小程序的旅游信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
Q_Q5110082854 小时前
python基于web的汽车班车车票管理系统/火车票预订系统/高铁预定系统 可在线选座
spring boot·python·django·flask·node.js·汽车·php
Q_Q51100828513 小时前
python+django/flask婚纱摄影拍照管理系统
spring boot·python·django·flask·node.js·php
Q_Q5110082851 天前
python+uniapp基于微信小程序的心理咨询信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
Q_Q5110082852 天前
python+uniapp基于微信小程序的学院设备报修系统
spring boot·python·微信小程序·django·flask·uni-app
liweiweili1262 天前
Django中处理多数据库场景
数据库·python·django
workflower2 天前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
局外人LZ3 天前
django rest framework:从零开始搭建RESTful API
python·django·restful·drf
weixin_421133413 天前
Django 的文档接口
python·django·sqlite
合作小小程序员小小店4 天前
web开发,学院培养计划系统,基于Python,FlaskWeb,Mysql数据库
后端·python·mysql·django·web app