【django学习-19】基本流程与用户管理界面(原始方式)
-
1.安装及创建项目
- 1.1:安装django,pip install django
- 1.2:创建项目:django-admin startproject 项目名称
- 1.3:创建app:python manage.py startapp app名称
- 1.4:使用pychram创建项目:
- 1.4.1:注意点,pycharm在标准的基础上默认给咱们加了点东西
- 1.4.2:创建了一个templates目录【删除】
- 1.4.3:settings.py中【删除】,DIRS列表内容
- 1.5:注册app
-
2.数据库操作
- 2.1:安装第三方模块:pip install mysqlclient
- 2.2:setting中配置数据库信息
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '', # 数据库名字
'USER': '',
'PASSWORD': '',
'HOST': '47.98.15.255', # 那台机器安装了MySQL
'PORT': 3306,
}
}
-
2.3:ORM可以帮助我们做两件事:
- 创建、修改、删除数据库中的表(不用你写SQL语句)。 【无法创建数据库】
- 操作表中的数据(不用写SQL语句)
-
2.4:创建数据库语句:create database gx_day02 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-
3.代码示例
- 3.1:user_add.html
{% extends 'layout.html' %}
{% load static %}
{% block css %}
<link rel="stylesheet" href="{% static 'plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css' %}">
{% endblock %}
{% block content %}
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"> 新建用户 </h3>
</div>
<div class="panel-body">
<form method="post">
{% csrf_token %}
<div class="form-group">
<label>姓名</label>
<input type="text" class="form-control" placeholder="姓名" name="user" />
</div>
<div class="form-group">
<label>密码</label>
<input type="text" class="form-control" placeholder="密码" name="pwd"/>
</div>
<div class="form-group">
<label>年龄</label>
<input type="text" class="form-control" placeholder="年龄" name="age"/>
</div>
<div class="form-group">
<label>余额</label>
<input type="text" class="form-control" placeholder="余额" name="ac"/>
</div>
<div class="form-group">
<label>入职时间</label>
<input type="text" id="dt" autocomplete="off" class="form-control" placeholder="入职时间" name="ctime"/>
</div>
<div class="form-group">
<label>性别</label>
<select class="form-control" name="gd">
{% for item in gender_choices %}
<option value="{{ item.0 }}">{{ item.1 }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label>部门</label>
<select class="form-control" name="dp">
{% for item in depart_list %}
<option value="{{ item.id }}">{{ item.title }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-primary">提 交</button>
</form>
</div>
</div>
</div>
{% endblock %}
- 3.2:urls.py
path('user/list/', user.user_list),
path('user/add/', user.user_add),
- 3.3:views.py
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm
def user_list(request):
""" 用户管理 """
queryset = models.UserInfo.objects.all()
# context = {
# "queryset": queryset,
# }
page_object = Pagination(request, queryset, page_size=10)
context = {
"queryset": page_object.page_queryset,
"page_string": page_object.html(),
}
return render(request, 'user_list.html', context)
def user_add(request):
""" 添加用户(原始方式) """
if request.method == "GET":
context = {
'gender_choices': models.UserInfo.gender_choices,
"depart_list": models.Department.objects.all()
}
return render(request, 'user_add.html', context)
# 获取用户提交的数据
user = request.POST.get('user')
pwd = request.POST.get('pwd')
age = request.POST.get('age')
account = request.POST.get('ac')
ctime = request.POST.get('ctime')
gender = request.POST.get('gd')
depart_id = request.POST.get('dp')
# 添加到数据库中
models.UserInfo.objects.create(name=user, password=pwd, age=age,
account=account, create_time=ctime,
gender=gender, depart_id=depart_id)
# 返回到用户列表页面
return redirect("/user/list/")
- 3.4:models.py
from django.db import models
class Department(models.Model):
""" 部门表 """
title = models.CharField(verbose_name='标题', max_length=32)
def __str__(self):
return self.title
class UserInfo(models.Model):
""" 员工表 """
name = models.CharField(verbose_name="姓名", max_length=16)
password = models.CharField(verbose_name="密码", max_length=64)
age = models.IntegerField(verbose_name="年龄")
account = models.DecimalField(verbose_name="账户余额", max_digits=10, decimal_places=2, default=0)
# create_time = models.DateTimeField(verbose_name="入职时间")
create_time = models.DateField(verbose_name="入职时间")
# 无约束
# depart_id = models.BigIntegerField(verbose_name="部门ID")
# 1.有约束
# - to,与那张表关联
# - to_field,表中的那一列关联
# 2.django自动
# - 写的depart
# - 生成数据列 depart_id
# 3.部门表被删除
# ### 3.1 级联删除
depart = models.ForeignKey(verbose_name="部门",to="Department", to_field="id", on_delete=models.CASCADE)
# ### 3.2 置空
# depart = models.ForeignKey(to="Department", to_field="id", null=True, blank=True, on_delete=models.SET_NULL)
# 在django中做的约束
gender_choices = (
(1, "男"),
(2, "女"),
)
gender = models.SmallIntegerField(verbose_name="性别", choices=gender_choices)