七、实验室简介模块 实验室简介模块主要用于实现实验室简介及学院其他实验室的基本情况介绍,主要包括滨海学院实验室简介、公共教学多媒体实验室、公共计算机实验室、计算机科学系专业实验室 、电子科学系专业实验室 、经济管理系专业实验室 、外语系专业实验室 、法政学系专业实验室 、环境科学与工程系专业实验室 、艺术系专业实验室 、金融学系专业实验室 、管理科学与工程系专业实验室等菜单,其中滨海学院实验室简介主要介绍实验室的整体情况,其他实验室均按照实验室的分类进行详细的介绍,其业务模块图如下:
1.模型设计 from django.db import models from DjangoUeditor.models import UEditorField import django.utils.timezone as timezone import time from loginApp.models import BMXXB,RYXXB
Create your models here.
m_y = (time.strftime("%y")) m_m = (time.strftime("%m"))
Create your models here.
#(三)实验室类型表(LABLXB) class LABLXB(models.Model): LABLXBH = models.CharField(‘实验室类型编号’,max_length=2,unique=True) LABLXMC = models.CharField(‘实验室类型名称’,max_length=50) XH = models.IntegerField(‘序号’,default=0) SYZT = models.IntegerField(‘使用状态’,default=0) CJSJ = models.DateTimeField(‘创建时间’,default = timezone.now)
class Meta:
verbose_name = '实验室类型维护'
verbose_name_plural = verbose_name
ordering = ("-CJSJ",)
def __str__(self):
return self.LABLXMC
#(十一) 实验室信息表(LABXXB) class LABXXB(models.Model): LABLXBH = models.ForeignKey(‘LABLXB’,on_delete=models.CASCADE,verbose_name=‘实验室类型编号’) LABBH = models.CharField(‘实验室编号’,max_length=50) LABMC = models.CharField(‘实验室名称’,max_length=100) LABMS = UEditorField(u’内容’, default=’’, width=1000, height=300, imagePath=‘lab/images/’+m_y+’/’+m_m+’/’, filePath=‘lab/files/’+m_y+’/’+m_m+’/’, upload_settings={‘imageMaxSizing’:1024000}, ) BMBH = models.ForeignKey(‘loginApp.BMXXB’,related_name=“labxx_bmbhs”,on_delete=models.CASCADE,verbose_name=‘部门编号’) VIEWS = models.PositiveIntegerField(‘访问量’,default=0) FBRR = models.ForeignKey(‘loginApp.RYXXB’,on_delete=models.CASCADE,verbose_name=‘发布人员’) CJSJ = models.DateTimeField(‘创建时间’,default = timezone.now)
class Meta:
verbose_name = '实验室信息维护'
verbose_name_plural = verbose_name
ordering = ("-CJSJ",)
def __str__(self):
return self.LABMC
2.迁移(编译)数据库 python manage.py makemigrations 3.创建数据库 python manage.py migrate
4.创建超级用户 python manage.py createsuperuser
5.后台管理设计
from django.contrib import admin from .models import *
Register your models here.
class LABXXBAdmin(admin.ModelAdmin): style_fields = {‘LABMS’: ‘ueditor’}
admin.site.register(LABXXB,LABXXBAdmin) admin.site.register(LABLXB) 6.视图设计
from django.shortcuts import render from .models import * from django.core.paginator import Paginator from django.shortcuts import get_object_or_404 from pyquery import PyQuery as pq
def labinfo(request): lablxs = LABLXB.objects.all().order_by(’-XH’) return render(request,‘labinfo.html’,{‘active_menu’:‘about’,‘lablxs’:lablxs,})
def labs(request,newName): # 解析请求的新闻类型 lablxs = LABLXB.objects.all().order_by(’-XH’) submenu = newName strname=’’ # print(newName) # if newName == ‘1’: # strname = ‘01’ # elif newName == ‘2’: # strname = ‘02’ # elif newName == ‘3’: # strname = ‘03’ # else: # strname = ‘09’ # print(strname) strname = newName lablxbh = LABLXB.objects.get(LABLXBH=strname).LABLXBH # print(lablxbh) lablxmc = LABLXB.objects.get(LABLXBH=strname).LABLXMC # print(lablxmc)
labList = LABXXB.objects.all().filter(LABLXBH=lablxbh).order_by('-CJSJ')
for labxx in labList:
html = pq(labxx.LABMS) # 使用pq方法解析html内容
labxx.mytxt = pq(html)('p').text() # 截取html段落文字
# 分页
p = Paginator(labList, 5)
if p.num_pages <= 1:
pageData = ''
else:
page = int(request.GET.get('page', 1))
labList = p.page(page)
left = []
right = []
left_has_more = False
right_has_more = False
first = False
last = False
total_pages = p.num_pages
page_range = p.page_range
if page == 1:
right = page_range[page:page + 2]
print(total_pages)
if right[-1] < total_pages - 1:
right_has_more = True
if right[-1] < total_pages:
last = True
elif page == total_pages:
left = page_range[(page - 3) if (page - 3) > 0 else 0:page - 1]
if left[0] > 2:
left_has_more = True
if left[0] > 1:
first = True
else:
left = page_range[(page - 3) if (page - 3) > 0 else 0:page - 1]
right = page_range[page:page + 2]
if left[0] > 2:
left_has_more = True
if left[0] > 1:
first = True
if right[-1] < total_pages - 1:
right_has_more = True
if right[-1] < total_pages:
last = True
pageData = {
'left': left,
'right': right,
'left_has_more': left_has_more,
'right_has_more': right_has_more,
'first': first,
'last': last,
'total_pages': total_pages,
'page': page,
}
# print(lablxs)
# print(labList)
return render(
request, 'labList.html', {
'active_menu': 'labs',
'sub_menu': submenu,
'lablxmc': lablxmc,
'lablxs':lablxs,
'labList': labList,
'pageData': pageData,
})
def labDetail(request, id): lablxs = LABLXB.objects.all().order_by(’-XH’)
mylab = get_object_or_404(LABXXB, id=id)
mylab.VIEWS += 1
mylab.save()
print(mylab)
return render(request, 'labDetail.html', {
'active_menu': 'labs',
# 'lablxmc': lablxmc,
'lablxs':lablxs,
'mylab': mylab,
})
7.访问路由设计 from django.urls import path from . import views
app_name = ‘aboutApp’
urlpatterns = [ path(‘labinfo/’, views.labinfo, name=‘labinfo’), # 实验室概况 path(‘labs/str:newName/’, views.labs, name=‘labs’),#实验室列表 path(‘labDetail/int:id/’, views.labDetail, name=‘labDetail’),#实验室详情 ] 8.实验室简介模板设计
{% extends “base.html” %} {% load static %}
{% block title %} 滨海学院实验室简介 {% endblock %}
{% block content %}
实验室介绍
南开大学滨海学院于2004年经教育部批准设立,学院建院以来十分重视实验室建设, 从建院至今,已投入九千多万元建设实验室。在建设中,根据教学科研和学科建设需要, 坚持“统筹规划、分步实施、资源共享、急用先建”的原则,使得学院各个实验室都能充分发挥效益。
目前学院针对各学科和专业特点建设有96个教学实验室,总面积为12516.47平方米,在用教学仪器设备6124件, 设备总值为6842.39万元,实验室专、兼职管理人员62名,年教学任务4614775人时数。
学院共有公共多媒体教室93间,公共计算机实验室11间,配备先进的计算机教学设备。 通过模拟、实操、演示、虚拟仿真等方式,开展多样化的实验教学课程。
{% endblock %} 9.实验室信息列表模板设计
{% extends “base.html” %} {% load static %}
{% block title %} {{lablxmc}} {% endblock %}
{% block content %}
{{lablxmc}}
{% for mylab in labList %}
<tr>
<td>
<div class="news-model">
<a href="{% url 'aboutApp:labDetail' mylab.id %}"><b>{{mylab.LABMC}}</b></a>
<span>【{{mylab.CJSJ|date:"Y-m-d"}}】</span>
<p>
<!-- 添加新闻简要说明 -->
摘要:{{mylab.mytxt|truncatechars:"110"}}...
</p>
</div>
</td>
</tr>
{% endfor %}
</table>
{% if pageData %}
<div class="paging">
<ul id="pages" class="pagination">
{% if pageData.first %}
<li><a href="?page=1">1</a></li>
{% endif %}
{% if pageData.left %}
{% if pageData.left_has_more %}
<li><span>...</span></li>
{% endif %}
{% for i in pageData.left %}
<li><a href="?page={{i}}">{{i}}</a></li>
{% endfor %}
{% endif %}
<li class="active"><a href="?page={{pageData.page}}">
{{pageData.page}}</a></li>
{% if pageData.right %}
{% for i in pageData.right %}
<li><a href="?page={{i}}">{{i}}</a></li>
{% endfor %}
{% if pageData.right_has_more %}
<li><span>...</span></li>
{% endif %}
{% endif %}
{% if pageData.last %}
<li><a href="?page={{pageData.total_pages}}">
{{pageData.total_pages}}</a></li>
{% endif %}
</ul>
</div>
{% endif %}
</div>
</div>
{% endblock %}
10.实验室详情模板设计 {% extends “base.html” %} {% load static %} {% block title %} {{mylab.LABLXBH}} {% endblock %} {% block content %}
<!-- 侧边导航栏 -->
<div class="col-md-3">
<div class="model-title">
{{mylab.LABLXBH}}
</div>
<div class="model-list">
<ul class="list-group">
<li class="list-group-item">
<a href="{% url 'aboutApp:labinfo'%}">滨海学院实验室简介</a>
</li>
</ul>
</div>
<div class="model-list">
<ul class="list-group">
{% for lablx in lablxs %}
<li class="list-group-item" id='{{lablx.LABLXMC}}'>
<a href="{% url 'aboutApp:labs' lablx.LABLXBH %}">{{lablx.LABLXMC}}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
<div class="col-md-9">
<div class="model-details-product-title">
{{mylab.LABMC}}
<div class="model-foot">发布时间:{{mylab.CJSJ|date:"Y-m-d"}}
浏览次数:{{mylab.VIEWS}}</div>
</div>
<div class="model-details">
{{ mylab.LABMS | safe }}
</div>
</div>
{% endblock %}
申明:本案例实现过程代码参照教材《Python Web开发从入门到实战》,部分代码存在直接引用,其详细介绍网站为:https://blog.csdn.net/qianbin3200896/article/details/106454741,如有版权问题,请联系本人,谢谢。
|