1. 数据库新建库-app[models.py]
| |
| class PrettyNum(models.Model): |
| '''手机号表''' |
| mobile = models.CharField(verbose_name="手机号", max_length=11) |
| |
| 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) |
| |
2. 执行命令生成数据库表
| |
| ''' |
| 1. 工具连接mysql生成数据库 |
| create database 库名 default charset utf8 collate utf8_general_ci; |
| |
| 2. django 中修改配置文件,连接Mysql |
| |
| 3. django命令生成数据库表 |
| python manage.py makemigrations |
| python manage.py migrate |
| ''' |
| |
| ```python |
| from django.shortcuts import render, redirect |
| |
| from app01 import models |
| |
| from app01.utils.form import PrettyModelForm, PrettyUpdateModelForm |
| |
| |
| |
| |
| def phone_list(request): |
| """列表""" |
| data_dict = {} |
| search_data = request.GET.get("q", "") |
| |
| if search_data: |
| data_dict["mobile__contains"] = search_data |
| |
| from app01.utils.pagemtion import Pagination |
| |
| |
| queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level") |
| |
| page_object = Pagination(request, queryset) |
| |
| context = { |
| "search_data": search_data, |
| |
| "phone": page_object.page_queryset, |
| "page_string": page_object.html() |
| } |
| |
| return render(request, "phone_list.html", context) |
| |
| |
| def phone_add(request): |
| if request.method == "GET": |
| """新增""" |
| form = PrettyModelForm() |
| return render(request, "phone_add.html", {"form": form}) |
| |
| form = PrettyModelForm(data=request.POST) |
| if form.is_valid(): |
| form.save() |
| return redirect("/phone/list/") |
| |
| return render(request, "phone_add.html", {"form": form}) |
| |
| |
| def phone_update(request, nid): |
| """更新用户""" |
| row_object = models.PrettyNum.objects.filter(id=nid).first() |
| if request.method == "GET": |
| form = PrettyUpdateModelForm(instance=row_object) |
| |
| return render(request, "phone_update.html", {"form": form}) |
| |
| form = PrettyUpdateModelForm(data=request.POST, instance=row_object) |
| |
| if form.is_valid(): |
| form.save() |
| return redirect("/phone/list/") |
| |
| return render(request, "phone_update.html", {"form": form}) |
| |
| |
| def phone_delete(request, nid): |
| '''删除靓号''' |
| models.PrettyNum.objects.filter(id=nid).delete() |
| return redirect("/phone/list/") |
| |
| from app01 import models |
| from app01.utils.bootstrap import BootStrapModelForm |
| from django import forms |
| |
| class PrettyModelForm(BootStrapModelForm): |
| |
| from django.core.validators import RegexValidator |
| mobile = forms.CharField( |
| label="手机号", |
| validators=[RegexValidator(r'1[3-9]\d{9}$', '手机号格式错误'), ], |
| ) |
| |
| class Meta: |
| model = models.PrettyNum |
| fields = ["mobile", "price", "level", "status"] |
| |
| |
| |
| |
| def clean_mobile(self): |
| from django.core.validators import ValidationError |
| txt = self.cleaned_data["mobile"] |
| exists = models.PrettyNum.objects.filter(mobile=txt).exists() |
| |
| if exists: |
| raise ValidationError("手机号已存在") |
| |
| |
| return txt |
| |
| |
| |
| class PrettyUpdateModelForm(BootStrapModelForm): |
| |
| from django.core.validators import RegexValidator |
| mobile = forms.CharField( |
| label="手机号", |
| validators=[RegexValidator(r'1[3-9]\d{9}$', '手机号格式错误'), ], |
| ) |
| |
| class Meta: |
| model = models.PrettyNum |
| fields = ["mobile", "price", "level", "status"] |
| |
| |
| def clean_mobile(self): |
| from django.core.validators import ValidationError |
| |
| |
| |
| |
| txt = self.cleaned_data["mobile"] |
| exists = models.PrettyNum.objects.exclude(id=self.instance.pk).filter(mobile=txt).exists() |
| |
| if exists: |
| raise ValidationError("手机号已jing存在") |
| |
| |
| return txt |
| |
5. 创建url,项目[url.py]
| |
| path('phone/list/', pretty.phone_list), |
| path('phone/add/', pretty.phone_add), |
| path('phone/<int:nid>/update/', pretty.phone_update), |
| path('phone/<int:nid>/delete/', pretty.phone_delete), |
6. 创建列表html,app[templates][phone_list.html]
| {% extends 'one.html'%} |
| |
| {% block content %} |
| <div class="container"> |
| <div style="margin-bottom:10px;" class="clearfix"> |
| <a class="btn btn-success" href="/phone/add/"> |
| <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> |
| 新建手机号</a> |
| |
| <div style="float:right;width:300px;"> |
| <form method="get"> |
| <div class="input-group"> |
| |
| <input type="text" name="q" class="form-control" placeholder="Search for..." |
| value="{{ search_data }}"> |
| <span class="input-group-btn"> |
| <button class="btn btn-default" type="submit"> |
| <span class="glyphicon glyphicon-search" aria-hidden="true"></span> |
| </button> |
| </span> |
| |
| </div> |
| </form> |
| </div> |
| |
| <a class="btn btn-success" href="/phone/add/"> |
| <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> |
| 新建手机号ModelForm</a> |
| </div> |
| |
| <div class="panel panel-default"> |
| |
| <div class="panel-heading"> |
| <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span> |
| 用户列表 |
| </div> |
| |
| |
| <table class="table table-bordered"> |
| <thead> |
| <tr> |
| <th>ID</th> |
| <th>手机号</th> |
| <th>价格</th> |
| <th>级别</th> |
| <th>状态</th> |
| </tr> |
| </thead> |
| <tbody> |
| {% for obj in phone %} |
| <tr> |
| <th>{{ obj.id }}</th> |
| <th>{{ obj.mobile }}</th> |
| <th>{{ obj.price }}</th> |
| |
| <td>{{ obj.get_level_display }}</td> |
| <td>{{ obj.get_status_display }}</td> |
| |
| <td> |
| <a class="btn btn-primary btn-xs" href="/phone/{{ obj.id }}/update">编辑</a> |
| <a class="btn btn-danger btn-xs" href="/phone/{{ obj.id }}/delete">删除</a> |
| </td> |
| </tr> |
| {% endfor %} |
| |
| </tbody> |
| </table> |
| </div> |
| <div class="clearfix"> |
| <ul class="pagination"> |
| {{ page_string }} |
| |
| </ul> |
| </div> |
| |
| |
| </div> |
| |
| {% endblock %} |
| |
7. 创建添加html,app[templates][phone_add.html]
| {% extends 'one.html' %} |
| |
| {% 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" novalidate> |
| {% csrf_token %} |
| |
| {% for field in form %} |
| <div class="form-group"> |
| <label>{{ field.label }}</label> |
| |
| {{ field }} |
| <span style="color:red;"> {{ field.errors.0 }}</span> |
| </div> |
| {% endfor %} |
| |
| <button type="submit" class="btn btn-primary">提 交</button> |
| </form> |
| </div> |
| </div> |
| </div> |
| |
| {% endblock %} |
| |
| |
8. 创建修改html,app[templates][phone_update.html]
| {% extends 'one.html' %} |
| |
| {% 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" novalidate> |
| {% csrf_token %} |
| |
| {% for field in form %} |
| <div class="form-group"> |
| <label>{{ field.label }}</label> |
| |
| {{ field }} |
| <span style="color:red;"> |
| {{ field.errors.0 }} |
| </span> |
| </div> |
| {% endfor %} |
| |
| <button type="submit" class="btn btn-primary">提 交</button> |
| </form> |
| </div> |
| </div> |
| </div> |
| |
| |
| {% endblock %} |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人