python制作web数据库系统_Python+Django+MySQL实现基于Web版建立筛选查找数据,展示数据,根据展示数据进行图表分析,柱状图,雷达图类
时间: 2024-02-09 19:08:26 浏览: 140
以下是一个简单的示例代码,使用Python和Django构建Web数据库系统,数据库使用MySQL,并实现数据筛选、展示和图表分析,包括柱状图和雷达图。
1. 安装Django和MySQL驱动
```
pip install django
pip install mysqlclient
```
2. 创建Django项目和应用
```
django-admin startproject webdb
cd webdb
python manage.py startapp myapp
```
3. 配置数据库
在`webdb/settings.py`文件中,将以下内容替换为你的MySQL数据库配置:
```
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
```
4. 创建模型
在`myapp/models.py`文件中,定义一个简单的模型:
```
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=50)
age = models.IntegerField()
gender = models.CharField(max_length=10)
def __str__(self):
return self.name
```
5. 迁移数据库
在终端中执行以下命令,创建表格:
```
python manage.py makemigrations
python manage.py migrate
```
6. 创建视图
在`myapp/views.py`文件中,定义数据筛选、展示和图表分析的视图:
```
from django.shortcuts import render
from django.db.models import Count
from .models import MyModel
def mymodel_list(request):
mymodels = MyModel.objects.all()
return render(request, 'myapp/mymodel_list.html', {'mymodels': mymodels})
def mymodel_filter(request):
if request.method == "POST":
gender = request.POST.get('gender')
mymodels = MyModel.objects.filter(gender=gender)
return render(request, 'myapp/mymodel_list.html', {'mymodels': mymodels})
else:
return render(request, 'myapp/mymodel_filter.html')
def mymodel_chart(request):
data = MyModel.objects.values('gender').annotate(count=Count('gender'))
labels = [item['gender'] for item in data]
values = [item['count'] for item in data]
return render(request, 'myapp/mymodel_chart.html', {'labels': labels, 'values': values})
```
7. 创建模板
在`myapp/templates/myapp`目录下,创建以下模板文件:
`mymodel_list.html`:
```
{% extends 'base.html' %}
{% block content %}
<h1>My Models</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
{% for mymodel in mymodels %}
<tr>
<td>{{ mymodel.name }}</td>
<td>{{ mymodel.age }}</td>
<td>{{ mymodel.gender }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
```
`mymodel_filter.html`:
```
{% extends 'base.html' %}
{% block content %}
<h1>Filter My Models</h1>
<form method="post">
{% csrf_token %}
<label for="gender">Gender:</label>
<select id="gender" name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<button type="submit">Filter</button>
</form>
{% endblock %}
```
`mymodel_chart.html`:
```
{% extends 'base.html' %}
{% block content %}
<h1>My Model Chart</h1>
<canvas id="myChart"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: {{ labels|safe }},
datasets: [{
label: 'Count',
data: {{ values|safe }},
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
],
borderWidth: 1
}]
}
});
</script>
{% endblock %}
```
`base.html`:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web Database System</title>
</head>
<body>
<div>
<a href="{% url 'mymodel_list' %}">My Models</a> | <a href="{% url 'mymodel_filter' %}">Filter</a> | <a href="{% url 'mymodel_chart' %}">Chart</a>
</div>
<hr>
{% block content %}
{% endblock %}
</body>
</html>
```
8. 配置URL路由
在`myapp/urls.py`文件中,定义URL路由:
```
from django.urls import path
from .views import mymodel_list, mymodel_filter, mymodel_chart
urlpatterns = [
path('', mymodel_list, name='mymodel_list'),
path('filter/', mymodel_filter, name='mymodel_filter'),
path('chart/', mymodel_chart, name='mymodel_chart'),
]
```
在`webdb/urls.py`文件中,将应用URL路由和Django默认URL路由合并:
```
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('myapp.urls')),
path('admin/', admin.site.urls),
]
```
9. 运行测试
在终端中执行以下命令,启动Django开发服务器:
```
python manage.py runserver
```
在浏览器中访问`http://localhost:8000/`,即可看到Web数据库系统界面,可以进行数据筛选、展示和图表分析操作。在筛选页面中选择性别,即可展示对应性别的数据;在图表页面中展示了性别统计的柱状图和雷达图。
阅读全文