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', ]