Django测试小工具平台开发(二)

一、编写django视图函数

Django 中的视图的概念是一类具有相同功能和模板的网页的集合。

作用就是将数据写入到html模板文件中,然后返回给调用方。

#django项目根目录下的views.py文文件中添加以下函数
def create_sales_leads(request):
    if request.method == 'POST':
        if request.POST:
            env = request.POST.get('env')   #环境控制
            Channel = request.POST.get('Channel')  
            originUserId = request.POST.get('originUserId').strip()
            teacherId = request.POST.get('teacherId').strip()
            num = int(request.POST.get('number').strip())  #控制产生的数据量
            template = loader.get_template('testtools/leads_detail.html')
            content ={
               'testtools':CreateDatabase().sales_leads(env,num,Channel,originUserId,teacherId),
                "type":2
            }
            #sales_leads函数为上一篇编写的功能函数,
            return HttpResponse(template.render(content))
            #使用django的render函数将数据放到模板中
        else:
            return HttpResponse('no params')
    else:
        return HttpResponse('method error!')

二、编写自定义的template模板

视图函数中使用了自定义的模板文件时就需要编写模板,django中有许多内置的模板也可以使用。

#django会自动在项目的templates中寻找模板文件,这里我在此文件加下创建testtools文件夹并将在文件加下创建名为leads_detail.html的模板文件
#leads_detail.html
{% extends 'testtools/base.html' %}
{% block content %}
{% if testtools %}
    {% if type == 1 %}
        <h3>普通流量例子数据</h3>
    {% elif type == 2 %}
        <h3>XXX例子数据</h3>
    {% elif type == 3 %}
        <h3>XXX例子数据</h3>     
    {% endif %}
    <ul>
       {% for leads in testtools %}
            <p style="font-size:15px">新建例子信息>>>>用户ID :  {{leads.userid }}  姓名 :  {{leads.name }} 手机号 :  {{leads.mobile }} </p>
        {%  endfor %}
    </ul>
 {% else %}
    <p>No user are available</p>
{% endif %}
​
{% endblock %}

说明:

​ 1、该模板是通用的模板,使用type参数控制显示的页面标题

​ 2、Django的模板可以实现继承该模板继承base.html

#base.html
<!-- base.html -->
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
    <style>
        #header {
            background-color: #00c4ff;
            color: white;
            text-align: center;
            padding: 5px;
        }
​
        #nav {
            line-height: 30px;
            background-color: #6C6C6C;
            height: 800px;
            width: auto;
            float: left;
            padding: 5px;
        }
​
        #section {
            width: 1000px;
            float: left;
            padding: 10px;
            height: 600px;
            padding-bottom: 1cm;
        }
​
        #footer {
            background-color: #00c4ff;
            color: white;
            clear: both;
            text-align: center;
            padding: 5px;
        }
​
        p.home {
            width: 180px;
            border: none;
            display: inline-block;
            outline: 0;
            padding: 8px 18px;
            margin-top: 15px;
            margin-bottom: 10px;
            vertical-align: middle;
            overflow: hidden;
            text-decoration: none;
            color: #fff;
            background-color: #e9686b;
            text-align: center;
            transition: .2s ease-out;
            cursor: pointer;
            white-space: nowrap;
            box-shadow: 0px 2px 6px rgb(0 0 0 / 12%), 0px 1px 2px rgb(0 0 0 / 24%);
            font-family: '微软雅黑';
            border-radius: 2px;
        }
        p.home2 {
            width: 100px;
            border: none;
            display: inline-block;
            outline: 0;
            padding: 8px 18px;
            margin-top: 15px;
            margin-bottom: 10px;
            vertical-align: middle;
            overflow: hidden;
            text-decoration: none;
            color: #fff;
            background-color: #e9686b;
            text-align: center;
            transition: .2s ease-out;
            cursor: pointer;
            white-space: nowrap;
            box-shadow: 0px 2px 6px rgb(0 0 0 / 12%), 0px 1px 2px rgb(0 0 0 / 24%);
            font-family: '微软雅黑';
            border-radius: 2px;
        }
        #g1 {
            width: 240px;
            border: none;
            display: inline-block;
            outline: 0;
            padding: 8px 18px;
            margin-top: 15px;
            margin-bottom: 10px;
            vertical-align: middle;
            overflow: hidden;
            text-decoration: none;
            color: #fff;
            background-color: #00b5f4;
            #text-align: center;
            transition: .2s ease-out;
            cursor: pointer;
            white-space: nowrap;
            box-shadow: 0px 2px 6px rgb(0 0 0 / 12%), 0px 1px 2px rgb(0 0 0 / 24%);
            font-family: '微软雅黑';
            border-radius: 2px;
        }
​
        a {
            color: #fff;
            text-decoration: none;
        }
        th {
            border-width: 1px;
            border-style: solid;
            border-color: #e6e6e6;
            position: relative;
            padding: 9px 15px;
            min-height: 20px;
            font-size: 14px;
            word-break: break-all;
            line-height: 20px;
            font-weight: 800;
            display: table-cell;
            vertical-align: inherit;
            text-align: center;
        }
        table {
            width: 80%;
            background-color: #fff;
            color: #666;
            border-collapse: collapse;
            border-spacing: 0;
            display: table;
            box-sizing: border-box;
            text-indent: initial;
            border-width: 1px;
            border-style: solid;
            border-color: #e6e6e6;
        }
        td {
            position: relative;
            padding: 9px 15px;
            min-height: 20px;
            line-height: 20px;
            font-size: 14px;
            border-width: 1px;
            border-style: solid;
            border-color: #e6e6e6;
            display: table-cell;
            vertical-align: inherit;
        }
    </style>
</head>
​
<body style="height: 100%;">
​
<div id="header">
    <h1>CRM系统测试小工具</h1>
</div>
​
<div id="nav">
    <a href="/testtools/">
        <p class="home2">首页</p>
    </a>
    <br>
    <a href="/testtools/home/">
        <p class="home2">导航</p>
    </a><br>
    <a href="/testtools/AccountList/">
        <p class="home2">验收</p>
    </a>
</div>
<div id="section">
    <h2>CRM例子系统</h2>
    {% block content %}
    {% endblock %}
</div>
<div id="footer">
    中后台研发中心-CRM测试组
</div>
</body>
</html>

base.html为整个页面的布局,leads_detail.html文件内容会代 入{% block content %} {% endblock %}中。

HTML、CSS现学现卖丑了点但是能用~~~~

 

三、设置路由

在项目根目录中找到url.py文件

在文件中添加 path('sales_leads/', views.sales_leads, name='sales_leads'),

urlpatterns = [
    path('', views.index, name='index'),
    path('sales_leads/', views.sales_leads, name='sales_leads'),
]

四、启动项目进行调试

python manage.py runserver   #启动项目

启动项目后可以使用浏览器或者postman进行调试

地址:http://127.0.0.1:8000/testtools/sales_leads/

请求方式:post

参数:

env

Channel

originUserId

teacherId

number

 

欢迎大家关注我的订阅号,会定期分享一些关于测试相关的文章,有问题也欢迎一起讨论学习!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值