django
下载
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple django
查看版本
django-admin --version
4.2.1
创建项目
django-admin startproject project
创建一个 app
python manage.py startapp app
注册 app
project/settings
# app名称.apps.app名称Config
INSTALLED_APPS = [
...,
'app.apps.AppConfig',
]
编写 url
project/urls
from app import views
urlpatterns = [
path('index/', views.index),
]
编写视图函数
app/views
from django.shortcuts import render, HttpResponse
def index(request):
return HttpResponse('index 函数')
启动项目
如果首次启动
python manage.py migrate
python manage.py runserver
使用 templates
app/views.py
def user_list(request):
return render(request, 'user_list.html')
使用静态文件
需要重新启动才会显示图片
app/static/image/xm.png
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>use_list</h1>
<img src="{% static 'image/xm.png' %}" alt="" />
</body>
</html>
常用模板语法
app/views.py
def user_list(request):
name = 'zs'
list = ['zs', 'ls', 'xr']
dict = {"name":"xr"}
flag = False
return render(request, 'user_list.html', {
"name": name,
"list": list,
"dict": dict,
"flag": flag
})
app/templates/user_list.html
<div>
<p>{{ list.0 }}</p>
{% for it in list %}
<p>{{ it }}</p>
{% endfor %}
</div>
<div>
{% for it in dict %}
<p>{{ it }}</p>
{% endfor %}
</div>
<div>
{% if flag %}
<p>True</p>
{% else %}
<p>False</p>
{% endif %}
</div>
请求与响应
app/views.py
from django.shortcuts import render, HttpResponse, redirect
def request(request):
# request是一个对象,封装了用户发送来的所有请求相关的数据
# method GET
# print('method', request.method)
# http://127.0.0.1:8000/request/?id=1&type=2 ==> GET <QueryDict: {'id': ['1'], 'type': ['2']}>
# print('GET', request.GET)
# print('POST', request.POST)
# 响应
# return HttpResponse('request')
# 重定向 是由浏览器重定向
# return redirect('http://www.baidu.com')
if request.method == 'GET':
return render(request, 'request.html')
# <QueryDict: {'csrfmiddlewaretoken': ['c5F6mUAlPwCx4sr4vrBUFmgahbFKntU5jzfsILC12ssT4wtSpwsniugHlHsVr7pH'], 'name': ['ad'], 'pwd': ['12']}>
# print('===',request.POST)
# return HttpResponse('登录成功')
name = request.POST.get('name')
pwd = request.POST.get('pwd')
if name == 'root' and pwd == '123':
return HttpResponse('登录成功')
return render(request, 'request.html', {'error_msg': "用户名密码错误"})
app/templates/request.html
<form action="/request/" method="post">
{% csrf_token %}
<input type="text" name="name" placeholder="用户名">
<input type="password" name="pwd" placeholder="密码">
<input type="submit" value="提交">
</form>
<p>{{ error_msg }}</p>
数据库操作
下载 mysqlclient
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mysqlclient
创建数据库
create database python
Django 链接数据库
替换为 mysql 数据库
project/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'python',
'USER':'root',
'PASSWORD':'123456',
'HOST':'127.0.0.1',
'PORT':'3306',
}
}
创建表和字段
app/models.py
class UserInfo(models.Model):
name = models.CharField(max_length=32)
password = models.CharField(max_length=64)
age = models.IntegerField()
执行命令
python manage.py makemigrations
python manage.py migrate
数据库基本操作
from app.models import UserInfo
def orm(request):
# 增
# UserInfo.objects.create(name='zs',password='123',age=18)
# UserInfo.objects.create(name='xr',password='123',age=28)
# 删
# UserInfo.objects.filter(id=1).delete()
# UserInfo.objects.all().delete()
# 查
# user_list = UserInfo.objects.all()
# user_list = UserInfo.objects.filter(id=2)
# dict = UserInfo.objects.filter(id=2).first()
# 改
UserInfo.objects.filter(id=2).update(age=66)
return HttpResponse('orm')