文章目录
前言
基于django前端,并于后端数据库交互,通过百度开源的Echarts渲染图标进行数据可视化的系统设计与实现,同时兼顾登录注册功能和基本的用户增删改查。
前提
已经使用django,创建了一个django项目。
一、主要功能模块代码
介绍:Django 是基于Python 的一种Web框架,该框架是以MVT架构的前端展示。
下面开始编写项目代码:
1.Views.py——视图函数:主要功能是编写函数方法,与Urls(路由)的连接实现方法的实现
1.引入所需库
from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from myapp.models import login_model,age_1_model,sex_model,fav_model,foodtype_age_model,foodtype_model,foodtype_fav_model,foodtype_sex_model,user_model
from django.core.paginator import Paginator
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
其中第三列,导入的为项目中models文件下的模型
2.编写用户注册和登录模块
# 登录页面
def loging(request):
#request.session['uipuser'] = 'lin'
print(request.session.get('vipuser',None))
return render(request,"cslogin/loging.html")
# 跳转到首页
def dologin(request):
user = login_model.objects.get(username = request.POST['username'])
if user.state == 0:
if user.password == request.POST['userpwd']:
request.session['vipuser'] = user.username
return redirect(reverse('index'))
else:
context = {
"info":"密码错误"}
else:
context = {
"info":"非vip请先注册"}
return render(request,"cslogin/loging.html",context)
def addlogin_admin(request):
return render(request,"cslogin/addlogin.html")
def addlogin(request):
user = login_model()
user.username = request.POST['name']
user.password = request.POST['password']
user.state = request.POST['state']
user.save()
print("管理员注册成功")
return render(request,'cslogin/loging.html')
def logout(request):
del request.session['vipuser']
return render(request,"cslogin/loging.html")
3.编写购物信息模块
# 购物信息管理
def index(request):
userList = user_model.objects.all()
info = {
"userList":userList}
return render(request,"user/index.html",info)
def index_fen(request,pIndex):
userList = user_model.objects.all()
p = Paginator(userList,11)
if pIndex == " ":
pIndex="1"
list2 = p.page(pIndex)
plist = p.page_range
context = {
}
context['userList'] = list2
context["plist"] = plist
return render(request,"user/index.html",context)
def add(request):
return render(request,"user/add.html")
def insert(request):
userlist = user_model()
userlist.id = request.POST['id']
userlist.foodtype = request.POST['foodtype']
userlist.sex = request.POST['sex']
userlist.age = request.POST['age']
userlist.fav = request.POST['fav']
userlist.save()
info = {
"info":"添加用户成功"}
return render(request,'user/info.html',info)
def delete(request,uid):
ob = user_model.objects.get(id=uid)
ob.delete()
info = {
"info":"删除用户成功"}
return render(request,'user/info.html',info)
def edit(request,uid):
ob = user_model.objects.get(id=uid)
info = {
"user":ob}
return render(request,'user/edit.html',info)
def update(request):
userlist = user_model.objects.get(id=request.POST['id'])
userlist.foodtype = request.POST['foodtype']
userlist.sex = request.POST['sex']
userlist.age = request.POST['age']
userlist.fav = request.POST['fav']
userlist.save()
info = {
"info":"修改成功!"}
return render(request,"user/info.html",info)
4.编写与前端交互模块
# 前端显示统计结果页面
# index_age 返回页面
def index_age(request):
return render(request,"sj/index_age.html")
# index_sex 返回页面
def index_sex(request):
return render(request,"sj/index_sex.html")
# index_fav 返回页面
def index_fav(request):
return render(request,"sj/index_fav.html")
# index_foodtype 返回页面
def index_foodtype(request):
return render(request,"sj/index_foodtype.html")
# index_foodtype_age 返回页面
def index_foodtype_age(request):
return render(request,"sj/food/index_foodtype_age.html")
# index_foodtype_sex 返回页面
def index_foodtype_sex(request):
return render(request,"sj/food/index_foodtype_sex.html")
# index_foodtype_fav 返回页面
def index_foodtype_fav(request):
return render(request,"sj/food/index_foodtype_fav.html")
5. 与数据库交互取出数据模块
# 从MySQL中读取data,并通过Ajax返回给html界面
# 读取年龄
def age(request):
users = age_1_model.objects.all()
age = []
sum = []
for user in users:
age.append(user.age)
sum.append(user.sum)
user_dict = {
}
user_dict['age'] = age
user_dict['sum'] = sum
return JsonResponse(user_dict)
# 读取食品类型
def foodtype(request):
users = foodtype_model.objects.all()
foodtype = []
sum