123456xjy 6 months ago
parent 222d797189
commit d62f72960c

@ -16,7 +16,7 @@ class Admin(models.Model):
class Department(models.Model):
""" 部门表 """
"""电影类别"""
title = models.CharField(verbose_name='标题', max_length=32)
def __str__(self):
@ -24,29 +24,13 @@ class Department(models.Model):
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 级联删除
# ### 3.2 置空
# depart = models.ForeignKey(to="Department", to_field="id", null=True, blank=True, on_delete=models.SET_NULL)
# 在django中做的约束
gender_choices = (
(1, ""),
(2, ""),
@ -59,40 +43,9 @@ class Movie(models.Model):
desc = models.CharField(verbose_name='影片描述', max_length=256,default = '')
class PrettyNum(models.Model):
""" 靓号表 """
mobile = models.CharField(verbose_name="手机号", max_length=11)
# 想要允许为空 null=True, blank=True
price = models.IntegerField(verbose_name="价格", default=0)
level_choices = (
(1, "1级"),
(2, "2级"),
(3, "3级"),
(4, "4级"),
)
level = models.SmallIntegerField(verbose_name="级别", choices=level_choices, default=1)
status_choices = (
(1, "已占用"),
(2, "未使用")
)
status = models.SmallIntegerField(verbose_name="状态", choices=status_choices, default=2)
class Task(models.Model):
""" 任务 """
level_choices = (
(1, "紧急"),
(2, "重要"),
(3, "临时"),
)
level = models.SmallIntegerField(verbose_name="级别", choices=level_choices, default=1)
title = models.CharField(verbose_name="标题", max_length=64)
detail = models.TextField(verbose_name="详细信息")
# user_id
user = models.ForeignKey(verbose_name="负责人", to="Admin", on_delete=models.CASCADE)
#什么东西啊啊啊
class Order(models.Model):
@ -110,17 +63,4 @@ class Order(models.Model):
admin = models.ForeignKey(verbose_name="管理员", to="Admin", on_delete=models.CASCADE)
class Boss(models.Model):
""" 老板 """
name = models.CharField(verbose_name="姓名", max_length=32)
age = models.IntegerField(verbose_name="年龄")
img = models.CharField(verbose_name="头像", max_length=128)
#我哪里来的老板啊
class City(models.Model):
""" 城市 """
name = models.CharField(verbose_name="名称", max_length=32)
count = models.IntegerField(verbose_name="人口")
# 本质上数据库也是CharField自动保存数据。
img = models.FileField(verbose_name="Logo", max_length=128, upload_to='city/')

@ -49,7 +49,7 @@
<li><a href="/movie/list/">电影信息</a></li>
<li><a href="/depart/list/">电影分类</a></li>
<li><a href="/order/list/">订单中心</a></li>
<li><a href="#">留言管理</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">

@ -17,18 +17,7 @@ class UserModelForm(BootStrapModelForm):
fields = ["name", "password", "age", 'account', 'create_time', "gender"]
class PrettyModelForm(BootStrapModelForm):
# 验证方式1
mobile = forms.CharField(
label="手机号",
validators=[RegexValidator(r'^1[3-9]\d{9}$', '手机号格式错误'), ],
)
class Meta:
model = models.PrettyNum
# fields = "__all__"
# exclude = ['level']
fields = ["mobile", 'price', 'level', 'status']
class MovieModelForm(BootStrapModelForm):
class Meta:
@ -37,36 +26,5 @@ class MovieModelForm(BootStrapModelForm):
# 验证方式2
def clean_mobile(self):
txt_mobile = self.cleaned_data["mobile"]
exists = models.PrettyNum.objects.filter(mobile=txt_mobile).exists()
if exists:
raise ValidationError("手机号已存在")
# 验证通过,用户输入的值返回
return txt_mobile
class PrettyEditModelForm(BootStrapModelForm):
# mobile = forms.CharField(disabled=True, label="手机号")
mobile = forms.CharField(
label="手机号",
validators=[RegexValidator(r'^1[3-9]\d{9}$', '手机号格式错误'), ],
)
#这个是干嘛的
class Meta:
model = models.PrettyNum
fields = ['mobile', 'price', 'level', 'status']
# 验证方式2
def clean_mobile(self):
# 当前编辑的哪一行的ID
# print(self.instance.pk)
txt_mobile = self.cleaned_data["mobile"]
exists = models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt_mobile).exists()
if exists:
raise ValidationError("手机号已存在")
# 验证通过,用户输入的值返回
return txt_mobile

@ -1,123 +0,0 @@
from django.shortcuts import render
from django.http import JsonResponse
def chart_list(request):
""" 数据统计页面 """
return render(request, 'chart_list.html')
def chart_bar(request):
""" 构造柱状图的数据 """
# 数据可以去数据库中获取
legend = ["梁吉宁", "武沛齐"]
series_list = [
{
"name": '梁吉宁',
"type": 'bar',
"data": [15, 20, 36, 10, 10, 10]
},
{
"name": '武沛齐',
"type": 'bar',
"data": [45, 10, 66, 40, 20, 50]
}
]
x_axis = ['1月', '2月', '4月', '5月', '6月', '7月']
result = {
"status": True,
"data": {
'legend': legend,
'series_list': series_list,
'x_axis': x_axis,
}
}
return JsonResponse(result)
def chart_pie(request):
""" 构造饼图的数据 """
db_data_list = [
{"value": 2048, "name": 'IT部门'},
{"value": 1735, "name": '运营'},
{"value": 580, "name": '新媒体'},
]
result = {
"status": True,
"data": db_data_list
}
return JsonResponse(result)
def chart_line(request):
legend = ["上海", "广西"]
series_list = [
{
"name": '上海',
"type": 'line',
"stack": 'Total',
"data": [15, 20, 36, 10, 10, 10]
},
{
"name": '广西',
"type": 'line',
"stack": 'Total',
"data": [45, 10, 66, 40, 20, 50]
}
]
x_axis = ['1月', '2月', '4月', '5月', '6月', '7月']
result = {
"status": True,
"data": {
'legend': legend,
'series_list': series_list,
'x_axis': x_axis,
}
}
return JsonResponse(result)
def highcharts(request):
""" highcharts示例 """
return render(request, 'highcharts.html')
from django.forms import ModelForm, Form
from django import forms
from app01 import models
# class TTModelForm(Form):
# name = forms.CharField(label="用户名")
# ff = forms.FileField(label="文件")
#
#
# def tt(request):
# if request.method == "GET":
# form = TTModelForm()
# return render(request, 'change.html', {"form": form})
# form = TTModelForm(data=request.POST, files=request.FILES)
# if form.is_valid():
# print(form.cleaned_data)
# return render(request, 'change.html', {"form": form})
class TTModelForm(ModelForm):
class Meta:
model = models.XX
fields = "__all__"
def tt(request):
instance = models.XX.objects.all().first()
if request.method == "GET":
form = TTModelForm(instance=instance)
return render(request, 'tt.html', {"form": form})
form = TTModelForm(data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return render(request, 'tt.html', {"form": form})

@ -1,32 +0,0 @@
from django.shortcuts import render, redirect
from app01 import models
from app01.utils.bootstrap import BootStrapModelForm
def city_list(request):
queryset = models.City.objects.all()
return render(request, 'city_list.html', {'queryset': queryset})
class UpModelForm(BootStrapModelForm):
bootstrap_exclude_fields = ['img']
class Meta:
model = models.City
fields = "__all__"
def city_add(request):
title = "新建城市"
if request.method == "GET":
form = UpModelForm()
return render(request, 'upload_form.html', {"form": form, 'title': title})
form = UpModelForm(data=request.POST, files=request.FILES)
if form.is_valid():
# 对于文件:自动保存;
# 字段 + 上传路径写入到数据库
form.save()
return redirect("/city/list/")
return render(request, 'upload_form.html', {"form": form, 'title': title})

@ -2,7 +2,7 @@ from django.shortcuts import render, redirect, HttpResponse
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm
from app01.utils.form import UserModelForm
def depart_list(request):

@ -2,7 +2,7 @@ from django.shortcuts import render, redirect
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm,MovieModelForm
from app01.utils.form import UserModelForm
def movie_list(request):
data_dict = {}

@ -1,59 +0,0 @@
from django.shortcuts import render, redirect
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm
def pretty_list(request):
""" 靓号列表 """
data_dict = {}
search_data = request.GET.get('q', "")
if search_data:
data_dict["mobile__contains"] = search_data
queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")
page_object = Pagination(request, queryset)
context = {
"search_data": search_data,
"queryset": page_object.page_queryset, # 分完页的数据
"page_string": page_object.html() # 页码
}
return render(request, 'pretty_list.html', context)
def pretty_add(request):
""" 添加靓号 """
if request.method == "GET":
form = PrettyModelForm()
return render(request, 'pretty_add.html', {"form": form})
form = PrettyModelForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('/pretty/list/')
return render(request, 'pretty_add.html', {"form": form})
def pretty_edit(request, nid):
""" 编辑靓号 """
row_object = models.PrettyNum.objects.filter(id=nid).first()
if request.method == "GET":
form = PrettyEditModelForm(instance=row_object)
return render(request, 'pretty_edit.html', {"form": form})
form = PrettyEditModelForm(data=request.POST, instance=row_object)
if form.is_valid():
form.save()
return redirect('/pretty/list/')
return render(request, 'pretty_edit.html', {"form": form})
def pretty_delete(request, nid):
models.PrettyNum.objects.filter(id=nid).delete()
return redirect('/pretty/list/')

@ -1,60 +0,0 @@
import json
from django import forms
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from app01 import models
from app01.utils.bootstrap import BootStrapModelForm
from app01.utils.pagination import Pagination
class TaskModelForm(BootStrapModelForm):
class Meta:
model = models.Task
fields = "__all__"
widgets = {
# "detail": forms.Textarea,
"detail": forms.TextInput
}
def task_list(request):
""" 任务列表 """
# 去数据库获取所有的任务
queryset = models.Task.objects.all().order_by('-id')
page_object = Pagination(request, queryset)
form = TaskModelForm()
context = {
"form": form,
"queryset": page_object.page_queryset, # 分完页的数据
"page_string": page_object.html() # 生成页码
}
return render(request, "task_list.html", context)
@csrf_exempt
def task_ajax(request):
print(request.GET)
print(request.POST)
data_dict = {"status": True, 'data': [11, 22, 33, 44]}
return HttpResponse(json.dumps(data_dict))
@csrf_exempt
def task_add(request):
# {'level': ['1'], 'title': ['sdfsdfsdfsd'], 'detail': ['111'], 'user': ['8']}
# print(request.POST)
# 1.用户发送过来的数据进行校验ModelForm进行校验
form = TaskModelForm(data=request.POST)
if form.is_valid():
form.save()
data_dict = {"status": True}
return HttpResponse(json.dumps(data_dict))
data_dict = {"status": False, 'error': form.errors}
return HttpResponse(json.dumps(data_dict, ensure_ascii=False))

@ -1,94 +0,0 @@
import os
from django.conf import settings
from django.shortcuts import render, HttpResponse
from app01 import models
def upload_list(request):
if request.method == "GET":
return render(request, 'upload_list.html')
# # 'username': ['big666']
# print(request.POST) # 请求体中数据
# # {'avatar': [<InMemoryUploadedFile: 图片 1.png (image/png)>]}>
# print(request.FILES) # 请求发过来的文件 {}
file_object = request.FILES.get("avatar")
# print(file_object.name) # 文件名WX20211117-222041@2x.png
f = open(file_object.name, mode='wb')
for chunk in file_object.chunks():
f.write(chunk)
f.close()
return HttpResponse("...")
from django import forms
from app01.utils.bootstrap import BootStrapForm, BootStrapModelForm
class UpForm(BootStrapForm):
bootstrap_exclude_fields = ['img']
name = forms.CharField(label="姓名")
age = forms.IntegerField(label="年龄")
img = forms.FileField(label="头像")
def upload_form(request):
title = "Form上传"
if request.method == "GET":
form = UpForm()
return render(request, 'upload_form.html', {"form": form, "title": title})
form = UpForm(data=request.POST, files=request.FILES)
if form.is_valid():
# {'name': '武沛齐', 'age': 123, 'img': <InMemoryUploadedFile: 图片 1.png (image/png)>}
# 1.读取图片内容,写入到文件夹中并获取文件的路径。
image_object = form.cleaned_data.get("img")
# media_path = os.path.join(settings.MEDIA_ROOT, image_object.name)
media_path = os.path.join("media", image_object.name)
f = open(media_path, mode='wb')
for chunk in image_object.chunks():
f.write(chunk)
f.close()
# 2.将图片文件路径写入到数据库
models.Boss.objects.create(
name=form.cleaned_data['name'],
age=form.cleaned_data['age'],
img=media_path,
)
return HttpResponse("...")
return render(request, 'upload_form.html', {"form": form, "title": title})
from django import forms
from app01.utils.bootstrap import BootStrapModelForm
class UpModelForm(BootStrapModelForm):
bootstrap_exclude_fields = ['img']
class Meta:
model = models.City
fields = "__all__"
def upload_modal_form(request):
""" 上传文件和数据modelForm"""
title = "ModelForm上传文件"
if request.method == "GET":
form = UpModelForm()
return render(request, 'upload_form.html', {"form": form, 'title': title})
form = UpModelForm(data=request.POST, files=request.FILES)
if form.is_valid():
# 对于文件:自动保存;
# 字段 + 上传路径写入到数据库
form.save()
return HttpResponse("成功")
return render(request, 'upload_form.html', {"form": form, 'title': title})

@ -2,8 +2,7 @@ from django.shortcuts import render, redirect
from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm
from app01.utils.form import UserModelForm
def user_list(request):
""" 用户管理 """

@ -18,15 +18,11 @@ from django.urls import path, re_path
from django.views.static import serve
from django.conf import settings
from app01.views import depart, user, pretty, admin, account, task, order, chart, upload,city,movie
from app01.views import depart, user, admin, account, order,movie
urlpatterns = [
# path('admin/', admin.site.urls),
re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
path('tt/', chart.tt),
# 部门管理
path('depart/list/', depart.depart_list),
path('depart/add/', depart.depart_add),
@ -41,11 +37,7 @@ urlpatterns = [
path('user/<int:nid>/edit/', user.user_edit),
path('user/<int:nid>/delete/', user.user_delete),
# 账号管理
path('pretty/list/', pretty.pretty_list),
path('pretty/add/', pretty.pretty_add),
path('pretty/<int:nid>/edit/', pretty.pretty_edit),
path('pretty/<int:nid>/delete/', pretty.pretty_delete),
# 管理员的管理
path('admin/list/', admin.admin_list),
@ -59,10 +51,6 @@ urlpatterns = [
path('logout/', account.logout),
path('image/code/', account.image_code),
# 任务管理
path('task/list/', task.task_list),
path('task/ajax/', task.task_ajax), # 学习Ajax
path('task/add/', task.task_add),
# 订单管理
path('order/list/', order.order_list),
@ -72,20 +60,10 @@ urlpatterns = [
path('order/edit/', order.order_edit),
# 数据统计
path('chart/list/', chart.chart_list),
path('chart/bar/', chart.chart_bar),
path('chart/pie/', chart.chart_pie),
path('chart/line/', chart.chart_line),
path('chart/highcharts/', chart.highcharts),
# 上传文件
path('upload/list/', upload.upload_list),
path('upload/form/', upload.upload_form),
path('upload/modal/form/', upload.upload_modal_form),
# 城市列表
path('city/list/', city.city_list),
path('city/add/', city.city_add),
# 电影信息管理
path('movie/list/',movie.movie_list),

Loading…
Cancel
Save