
一.項(xiàng)目背景
本項(xiàng)目利用DjangoWeb框架制作一個(gè)簡(jiǎn)易版的網(wǎng)頁(yè)查詢(xún)數(shù)據(jù),數(shù)據(jù)源為Excel表格
數(shù)據(jù),通過(guò)該項(xiàng)目讓大家了解到Django與辦公自動(dòng)化的結(jié)合,也給大家拓展一種數(shù)據(jù)展
示思路。
二.實(shí)現(xiàn)過(guò)程
1.創(chuàng)建項(xiàng)目
1)創(chuàng)建項(xiàng)目(django-admin startproject 項(xiàng)目名稱(chēng))和app(python manage.py startapp myapp)
2)在settings.py中添加配置信息
3)啟動(dòng)項(xiàng)目(python manage.py runserver)
"""
Django settings for data_test project.
Generated by 'django-admin startproject' using Django 1.11.4.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'w_!tgu=!e^z0i)beg=1(s-7p*t)1-494@w#^j-jb6(^vz$)n!3'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#注冊(cè)app
'myapp'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'data_test.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#添加templates模板路徑
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'data_test.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
#配置靜態(tài)文件
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
【注】以上是settings.py文件中代碼,有中文注釋處是本次項(xiàng)目所添加代碼。

2.獲取數(shù)據(jù)
1)read_excel讀取Excel數(shù)據(jù)(如下圖所示)
2)前端構(gòu)建form表單
3)獲取頁(yè)面?zhèn)鬟^(guò)來(lái)的參數(shù)

{% load static %}
{% csrf_token %}
姓名: {% if stu_name %} {%else %} {% endif %}
{{ data|safe }}
【注】以上是test.html文件中代碼
from django.shortcuts import render
import pandas as pd
#獲取數(shù)據(jù)
def get_data(request):
#讀取excel文件
data=pd.read_excel('test.xlsx')
#如果請(qǐng)求方式是POST請(qǐng)求
if request.method=='POST':
#獲取輸入值
value=request.POST.get('name')
#判斷是否為空
if value=='':
#如果為空,返回原始數(shù)據(jù),并且將前臺(tái)頁(yè)面輸入置為空
return render(request, 'test.html', {'data': data.to_html(index=False),'stu_name':''})
else:
#查取姓名,此處為模糊查詢(xún)
data_query=data[data['姓名'].str.contains(value)]
#獲取數(shù)據(jù),將查到的數(shù)據(jù)和輸入框值返回頁(yè)面
return render(request,'test.html',{'data':data_query.to_html(index=False),'stu_name':value})
else:
#如果是GET請(qǐng)求,直接返回所有數(shù)據(jù)
return render(request, 'test.html', {'data':data.to_html(index=False)})
【注】以上是views.py文件中代碼
3.展示數(shù)據(jù)
1)配置url(如下圖)
2)配置css文件
2)表格可視化

.data{
text-align:center
}
.data_query{
align:center
}
【注】以上是test.css文件中代碼
查詢(xún)前結(jié)果展示

模糊查詢(xún)后結(jié)果展示

-
Web
+關(guān)注
關(guān)注
2文章
1279瀏覽量
70647 -
Excel
+關(guān)注
關(guān)注
4文章
225瀏覽量
56289 -
數(shù)據(jù)源
+關(guān)注
關(guān)注
1文章
65瀏覽量
9852
發(fā)布評(píng)論請(qǐng)先 登錄
基于LabVIEW的excel文件讀取與數(shù)據(jù)查詢(xún)
想通過(guò)點(diǎn)擊一個(gè)按鈕生成一個(gè)excel表格 數(shù)據(jù)跟數(shù)據(jù)查詢(xún)的數(shù)據(jù)是一樣的。
Labview讀取excel信息時(shí)怎么查詢(xún)檢索某一個(gè)用戶(hù)的全部信息?
如何用阿里云的Iot Studio制作web網(wǎng)頁(yè)呢
智龍EXCEL電氣報(bào)價(jià)軟件9.0
網(wǎng)頁(yè)制作課件,下載(免費(fèi))
電機(jī)系網(wǎng)頁(yè)制作
Excel2003表格制作教程下載

網(wǎng)頁(yè)制作基礎(chǔ)
PHP網(wǎng)頁(yè)制作的經(jīng)典試題資料合集免費(fèi)下載

微軟網(wǎng)頁(yè)版Excel新增復(fù)制粘貼功能,助力提升用戶(hù)工作效率
根據(jù)ip地址查網(wǎng)頁(yè)怎么查詢(xún)?

評(píng)論