Django创建数据库
配置settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'userinfor',
'USER': 'root',
'PASSWORD': 'lztv',
'HOST': '127.0.0.1',
'PORT': 3306
}
}
创建表格
- models.py文件
from django.db import models
class user(models.Model):
account = models.CharField(max_length=64, primary_key=True)
passwd = models.CharField(max_length=16)
注册
- admin.py文件
from .models import *
admin.site.register(user)
同步数据库
- 初始化
python manage.py makemigrations
- 迁移
python manage.py migrate
数据库可视化
- 创建超级管理员
python manage.py createsuperuser
- 设置管理员
输入用户名,跳过邮箱,输入密码(两次输入)
浏览器查看
- 打开浏览器输入
http://127.0.0.1:8000/admin
操作Mysql
- 查询所有
models.user.objects.all()
- 只取account列
models.user.objects.all().values('account')
- 取出acount和password列,并生成一个列表
models.user.objects.all().values_list('account','password')
- get方法获取
models.user.objects.get(account='lz')
models.user.objects.get(password='lztv')
导包管理
models.py
from django.db import models
views.py
# json解析
import json
# 渲染网页
from django.shortcuts import render
# 网页回应
from django.http import HttpResponse
# 导入数据模型
from .models import *
# csrf验证
from django.views.decorators.csrf import csrf_exempt
admin.py
from django.contrib import admin
# 导入数据模型
from .models import *
操作mysql
查询
@csrf_exempt
def select(request):
if request.method == 'POST':
select = request.POST.get('select')
if select == 'all':
result = Account.objects.all()
data = {}
for obj in result:
account = obj.account
password = obj.password
data[account] = password
return HttpResponse(json.dumps({'code': 200, 'msg': json.dumps(data)}))
elif select == 'not_all':
account = request.POST.get('account')
if len(account) != 0:
result = Account.objects.filter(account=account).first()
if result is not None:
password = result.password
data = {account:password}
return HttpResponse(json.dumps({'code': 200, 'msg': json.dumps(data)}))
else:
return HttpResponse(json.dumps({'code': 400, 'msg': '未查询到此用户!'}))
else:
return HttpResponse(json.dumps({'code': 400, 'msg': '账户名不能为空!'}))
添加
@csrf_exempt
def add_data(request):
if request.method == 'POST':
account = request.POST.get('account')
if Account.objects.filter(account=account).count() == 0:
password = request.POST.get('password')
Account.objects.create(account=account, password=password)
return HttpResponse(json.dumps({'code': 200, 'msg': '添加成功!'}))
else:
return HttpResponse(json.dumps({'code': 400, 'msg': '此用户已存在!'}))
更新
@csrf_exempt
def update_infor(request):
if request.method == 'POST':
account = request.POST.get('account')
obj = Account.objects.filter(account=account)
if obj is not None:
obj.update(password='admin')
return HttpResponse(json.dumps({'code': 200, 'msg': '密码重置为admin'}))
else:
return HttpResponse(json.dumps({'code': 400, 'msg': '重置失败!'}))
删除
@csrf_exempt
def delect_infor(request):
if request.method == 'POST':
account = request.POST.get('account')
obj = Account.objects.get(account=account)
if obj is not None:
obj.delete()
return HttpResponse(json.dumps({'code': 200, 'msg': '删除信息成功!'}))
else:
return HttpResponse(json.dumps({'code': 400, 'msg': '删除失败!'}))
修改models
增加字段只需重新初始化,以下针对修改表结构
-
删除app下的migrate文件夹
-
数据库修改
如果已存在的数据无意义且数据量不大,删除数据库,重建
不删除数据库,打开相应的数据库,删除app的相关信息
delete from django_migrations where app = 'app_name';
网友评论