1.打开终端创建数据库 Django_ORM,在settings.py配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Django_ORM', #你的数据库名称
'USER': 'root', #你的数据库用户名
'PASSWORD': 'root', #你的数据库密码
'HOST': '', #你的数据库主机,留空默认为localhost
'PORT': '3306', #你的数据库端口
}
}
2.配置__init__.py
import pymysql
pymysql.install_as_MySQLdb()
3.在models.py创建表
from django.db import models
# Create your models here.
class Book(models.Model):
name=models.CharField(max_length=20)
price=models.IntegerField()
pub_date=models.DateField()
author=models.CharField(max_length=32,null=False)
def __str__(self):
return self.name
class Author(models.Model):
name=models.CharField(max_length=32)
4.配置urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path('addbook/', views.addbook),
path('update/', views.update),
path('delete/', views.delete),
path('select/', views.select),
]
5.写views.py
from django.shortcuts import render,HttpResponse
from app01.models import *
# Create your views here.
def index(request):
return render(request,"index.html")
def addbook(request):
#方式一
# b=Book(name="python基础",price=99,author="zhangyafeng",pub_date="2018-3-24")
# b.save()
#方式二
Book.objects.create(name="老男孩linux",price=78,author="oldboy",pub_date="2018-3-25")
return HttpResponse("添加成功")
def update(request):
#第一种方式(推荐使用)
#Book.objects.filter(author="zhangyafeng").update(price=100)
#第二种方式(get只能修改一个)
b=Book.objects.get(id=3)
b.price=999
b.save()
return HttpResponse("修改成功")
def delete(request):
Book.objects.filter(id=2).delete()
return HttpResponse("删除成功")
def select(request):
# book_list=Book.objects.filter().all()
# print(book_list)
# print(book_list[0])
#取出一个但是是可以迭代的
#book_list = Book.objects.filter(id=2)
#book_list = Book.objects.filter().all()[::2]
# book_list = Book.objects.filter().all()[::-1]
# book_list = Book.objects.filter().all().first()
# book_list = Book.objects.filter().all().last()
#取出的只有一个(如果取出多条记录会报错)
#book_list = Book.objects.filter().get(id=2)
#只单查name和price的集合
#book_list=Book.objects.filter(author="zhangyafeng").values("name","price")
#此时打印为列表
#book_list=Book.objects.filter(author="zhangyafeng").values_list("name","price")
#不包含
#book_list=Book.objects.exclude(author="zhangyafeng").values("name","price")
#去重复
# book_list = Book.objects.all().distinct().values("name")
#
# counts = Book.objects.all().distinct().count()
# print(counts)
#模糊查询 万能的双下划线 __
book_list=Book.objects.filter(price__gt=50).values("name","price")
book_list=Book.objects.filter(name__icontains="p").values("name","price")
return render(request,"index.html",locals())
6.创建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
.head{
line-height: 40px;
background-color: green;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div class="outer">
<div class="head">标题</div>
<div class="content">
<a href="/addbook/">添加数据</a>
<a href="/update/">修改数据</a>
<a href="/delete/">删除数据</a>
<a href="/select/">查询数据</a>
</div>
<div class="querResult">
{% for book in book_list %}
<div>
<p>{{ book.name }} {{ book.author }} {{ book.price }}</p>
</div>
{% endfor %}
</div>
</div>
</body>
</html>
测试运行
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 8080