python制作web数据库系统_Python+Django+MySQL实现基于Web版的增删改查的示例代码
时间: 2023-12-29 13:03:12 浏览: 132
好的,以下是一个简单的示例代码,使用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()
def __str__(self):
return self.name
```
5. 迁移数据库
在终端中执行以下命令,创建表格:
```
python manage.py makemigrations
python manage.py migrate
```
6. 创建视图
在`myapp/views.py`文件中,定义增删改查的视图:
```
from django.shortcuts import render, get_object_or_404, redirect
from .models import MyModel
from .forms import MyModelForm
def mymodel_list(request):
mymodels = MyModel.objects.all()
return render(request, 'myapp/mymodel_list.html', {'mymodels': mymodels})
def mymodel_detail(request, pk):
mymodel = get_object_or_404(MyModel, pk=pk)
return render(request, 'myapp/mymodel_detail.html', {'mymodel': mymodel})
def mymodel_new(request):
if request.method == "POST":
form = MyModelForm(request.POST)
if form.is_valid():
mymodel = form.save(commit=False)
mymodel.save()
return redirect('mymodel_detail', pk=mymodel.pk)
else:
form = MyModelForm()
return render(request, 'myapp/mymodel_edit.html', {'form': form})
def mymodel_edit(request, pk):
mymodel = get_object_or_404(MyModel, pk=pk)
if request.method == "POST":
form = MyModelForm(request.POST, instance=mymodel)
if form.is_valid():
mymodel = form.save(commit=False)
mymodel.save()
return redirect('mymodel_detail', pk=mymodel.pk)
else:
form = MyModelForm(instance=mymodel)
return render(request, 'myapp/mymodel_edit.html', {'form': form})
def mymodel_delete(request, pk):
mymodel = get_object_or_404(MyModel, pk=pk)
mymodel.delete()
return redirect('mymodel_list')
```
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></th>
</tr>
</thead>
<tbody>
{% for mymodel in mymodels %}
<tr>
<td>{{ mymodel.name }}</td>
<td>{{ mymodel.age }}</td>
<td><a href="{% url 'mymodel_detail' pk=mymodel.pk %}">Detail</a> | <a href="{% url 'mymodel_edit' pk=mymodel.pk %}">Edit</a> | <a href="{% url 'mymodel_delete' pk=mymodel.pk %}">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{% url 'mymodel_new' %}">New My Model</a>
{% endblock %}
```
`mymodel_detail.html`:
```
{% extends 'base.html' %}
{% block content %}
<h1>{{ mymodel.name }}</h1>
<p>Age: {{ mymodel.age }}</p>
<a href="{% url 'mymodel_edit' pk=mymodel.pk %}">Edit</a> | <a href="{% url 'mymodel_delete' pk=mymodel.pk %}">Delete</a>
{% endblock %}
```
`mymodel_edit.html`:
```
{% extends 'base.html' %}
{% block content %}
<h1>{% if form.instance.pk %}Edit{% else %}New{% endif %} My Model</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
{% 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>
</div>
<hr>
{% block content %}
{% endblock %}
</body>
</html>
```
8. 创建表单
在`myapp/forms.py`文件中,创建一个表单:
```
from django import forms
from .models import MyModel
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('name', 'age')
```
9. 配置URL路由
在`myapp/urls.py`文件中,定义URL路由:
```
from django.urls import path
from .views import mymodel_list, mymodel_detail, mymodel_new, mymodel_edit, mymodel_delete
urlpatterns = [
path('', mymodel_list, name='mymodel_list'),
path('<int:pk>/', mymodel_detail, name='mymodel_detail'),
path('new/', mymodel_new, name='mymodel_new'),
path('<int:pk>/edit/', mymodel_edit, name='mymodel_edit'),
path('<int:pk>/delete/', mymodel_delete, name='mymodel_delete'),
]
```
在`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),
]
```
10. 运行测试
在终端中执行以下命令,启动Django开发服务器:
```
python manage.py runserver
```
在浏览器中访问`http://localhost:8000/`,即可看到Web数据库系统界面,可以进行增删改查操作。
阅读全文