AI智能
改变未来

Django简易测试平台三:模块的增删改查


1、新建模块base

INSTALLED_APPS 增加模块名:base

2、路由设计

1)API/urls.py 配置: 新增如下内容
url(r’^base’, include(“base.urls”))
2)base app下新建urls.py内容如下:

from django.conf.urls import urlfrom base.views import *urlpatterns = [url(r\'/project/\', project_index),url(r\'/project_add/\', project_add),url(r\'/project_update/\', project_update),url(r\'/project_delete/\', project_delete),]

3、数据库模型设计

/base/models.py

#项目列表class Project(models.Model):prj_id = models.AutoField(primary_key=True, null=False)prj_name = models.CharField(max_length=50,null=True)sign = models.ForeignKey(\'Sign\', on_delete=models.CASCADE)description = models.CharField(max_length=100,null=True)#定制包列表class App(models.Model):app_id = models.AutoField(primary_key=True, null=False)app_name = models.CharField(max_length=50,null=True)app_url = models.CharField(max_length=1000,null=True)description = models.CharField(max_length=100,null=True)project = models.ForeignKey(\'Project\', on_delete=models.CASCADE)#加密class Sign(models.Model):sign_id = models.AutoField(primary_key=True, null=False)sign_name = models.CharField(max_length=50,null=True)description = models.CharField(max_length=100,null=True)#用例列表class Case(models.Model):case_id = models.AutoField(primary_key=True, null=False)case_name = models.CharField(max_length=50,null=True)project = models.ForeignKey(\'Project\', on_delete=models.CASCADE)description = models.CharField(max_length=200,null=True)content = models.TextField()

将修改好的新模型写入数据库

遇到问题:数据库模型更改没有在数据库中生效
解决方案:
(1)删除原来的迁移文件
目录如下项目名.app.migrations

(2)重新建立迁移文件
即重新python manage.py makemigrations app

(3)删除数据库记录
进入项目数据库,找到django_migrations表,把app为你app名字的那行记录删除,

(4)删除相关app的旧表
将关于你修改后的app的表全部删除

(5)将修改后的表加入数据库
再回来进行migrate,你会发现你修改后的模型已经成功的导入数据库。

4、视图内容

/base/views.py

def project_add(request):if request.method == \'POST\':prj_name = request.POST[\'prj_name\']name_same = Project.objects.filter(prj_name=prj_name)if name_same:messages.error(request, \"项目已存在\")else:description = request.POST[\'description\']sign_id = request.POST[\'sign\']sign = Sign.objects.get(sign_id=sign_id)prj = Project(prj_name=prj_name, description=description, sign=sign)prj.save()return HttpResponseRedirect(\"/base/project/\")sign_list = Sign.objects.all()return render(request, \"base/project/add.html\", {\"sign_list\": sign_list})def project_update(request):if request.method == \'POST\':prj_id = request.POST[\'prj_id\']prj_name = request.POST[\'prj_name\']name_exit = Project.objects.filter(prj_name=prj_name).exclude(prj_id=prj_id)if name_exit:# messages.error(request, \"项目已存在\")return HttpResponse(\"项目已存在\")else:description = request.POST[\'description\']sign_id = request.POST[\'sign_id\']sign = Sign.objects.get(sign_id=sign_id)Project.objects.filter(prj_id=prj_id).update(prj_name=prj_name, description=description,sign=sign)return HttpResponseRedirect(\"/base/project/\")prj_id = request.GET[\'prj_id\']prj = Project.objects.get(prj_id=prj_id)sign_list = Sign.objects.all()return render(request, \"base/project/update.html\", {\"prj\": prj, \"sign_list\": sign_list})def project_delete(request):if request.method == \'GET\':prj_id = request.GET[\'prj_id\']Project.objects.filter(prj_id=prj_id).delete()return HttpResponseRedirect(\"base/project/\")

add函数的关键内容

django 视图函数的request说明:

我们知道当URLconf文件(如base/urls)匹配到用户输入的路径后,会调用对应的view(如project_index)函数,并将 HttpRequest对象 作为第一个参数传入该函数。

Django 每一个view函数的第一个参数都是request,有没想过request里面到底有什么呢?

Django使用request和response对象在系统间传递状态。

当一个页面被请示时,Django创建一个包含请求元数据的 HttpRequest 对象。 然后Django调入合适的视图,把HttpRequest 作为视图函数的第一个参数 传入。每个视图要负责返回一个 HttpResponse 对象。

HttpRequest实例的属性包含了关于此次请求的大多数重要信息。 除了session外的所有属性都应该认为是只读的.

参考:Django基础必备三神装(HttpResponse、render,、redirect)https://blog.csdn.net/miaoqinian/article/details/80668003

5、模板/界面设计如图


project/add.html关键内容

遇到的问题

1、Django POST时错误提示CSRF验证失败

将setting文件中的 django.middleware.csrf.CsrfViewMiddleware 注释掉

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Django简易测试平台三:模块的增删改查