自动化测试平台的搭建(二)
1. 登录功能实现
views 添加login ,并在urls里注册
from django.shortcuts import render from django.http import HttpResponseRedirect,HttpResponse#加入引用 from django.contrib.auth.decorators import login_required from django.contrib import auth from django.contrib.auth import authenticate,login # Create your views here. def login(request): """实现登录功能""" if request.POST: username=password="" username=request.POST.get('username') password=request.POST.get('password') user=authenticate(username=username,password=password) if user is not None and user.is_active: auth.login(request,user) request.session['user']=username response=HttpResponseRedirect('/home/') return response else: return render(request,'login.html',{'error':'username or password error'}) return render(request,'login.html')
创建home.html并加入urls.py
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>自动化测试平台</title> </head> <body> <ul class="nav navbar-nav navbar-right"> <li>欢迎您,<a href="#">{{ user }}</a></li> <li><a href="/logout/">退出</a> </li> </ul> </body> </html>
def home(request): return render(request,'home.html') def logout(request): auth.logout(request) return render(request,'login.html')
2.产品管理模块的开发
数据库设计:
2.1 创建新的应用 python manage.py startapp product
2.2 根据数据库设计生成django admin后台功能,在product/admin.py 加入如下、
from django.contrib import admin from product.models import Product # Register your models here. class ProductAdmin(admin.ModelAdmin): list_display = ['productname','productdesc','producter','create_time','id'] admin.site.register(Product) #把产品模块注册到django admin后台并展示
2.3 在product/models.py中加入如下代码:
from django.db import models # Create your models here. class Product(models.Model): """产品""" productname=models.CharField('产品名称',max_length=64) productdesc = models.CharField('产品描述', max_length=64) producter = models.CharField('产品负责人', max_length=64) cteate_time = models.DateTimeField('创建时间', auto_now=True) # 自动获取当前时间 class Meta: # 设置迁移后的表名 db_table="Product" verbose_name='产品管理' verbose_name_plural='产品管理' def __str__(self): return self.productname
2.4 在autotest中的setting中加入product应用
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', #admin依赖 'django.contrib.contenttypes', #admin依赖 'django.contrib.sessions', #admin依赖 'django.contrib.messages', 'django.contrib.staticfiles', 'apitest', 'product', ]
2.5 同步数据库
python manage.py makemigrations
python manage.py migrate
产品管理功能前端开发:
使用bootstrap4
pip install django-bootstrap4
然后在setting.py加入
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', #admin依赖 'django.contrib.contenttypes', #admin依赖 'django.contrib.sessions', #admin依赖 'django.contrib.messages', 'django.contrib.staticfiles', 'apitest', 'product', 'bootstrap4', ]
目前还在学习中,希望会对大家有所帮助,觉得不错,就点赞支持一下。
另外,转载时请附带链接。谢谢!