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
相关推荐
markfeng83 天前
Python+Django+H5+MySQL项目搭建
python·django
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
starlaky4 天前
Django入门笔记
笔记·django
QQ5110082854 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe4 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js高考推荐系统 高考可视化 大数据毕业设计(源码+LW文档+PPT+详细讲解)
大数据·vue.js·hadoop·django·毕业设计·课程设计·推荐算法
计算机程序猿学长4 天前
大数据毕业设计-基于django的音乐网站数据分析管理系统的设计与实现(源码+LW+部署文档+全bao+远程调试+代码讲解等)
大数据·django·课程设计
B站计算机毕业设计超人4 天前
计算机毕业设计Django+Vue.js音乐推荐系统 音乐可视化 大数据毕业设计 (源码+文档+PPT+讲解)
大数据·vue.js·hadoop·python·spark·django·课程设计
B站_计算机毕业设计之家4 天前
电影知识图谱推荐问答系统 | Python Django系统 Neo4j MySQL Echarts 协同过滤 大数据 人工智能 毕业设计源码(建议收藏)✅
人工智能·python·机器学习·django·毕业设计·echarts·知识图谱