python制作web数据库系统_Python+Django+MySQL实现基于Web版的增删改查的示例代码
时间: 2023-12-29 15:03:23 浏览: 152
好的,以下是一个基于Python+Django+MySQL实现的Web数据库系统的示例代码:
1. 首先,需要安装Django和MySQL依赖库。可以通过以下命令进行安装:
```
pip install Django
pip install mysqlclient
```
2. 创建Django项目和应用,可以通过以下命令进行创建:
```
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
```
3. 在Django项目的settings.py中,配置MySQL数据库连接信息:
```
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '3306',
}
}
```
4. 在Django应用的models.py中,定义数据库模型:
```
from django.db import models
class User(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
age = models.IntegerField()
def __str__(self):
return self.name
```
5. 进行数据库迁移,创建User表:
```
python manage.py makemigrations
python manage.py migrate
```
6. 在Django应用的views.py中,编写增删改查的视图函数:
```
from django.shortcuts import render, get_object_or_404, redirect
from .models import User
from .forms import UserForm
def user_list(request):
users = User.objects.all()
return render(request, 'user_list.html', {'users': users})
def user_detail(request, pk):
user = get_object_or_404(User, pk=pk)
return render(request, 'user_detail.html', {'user': user})
def user_new(request):
if request.method == "POST":
form = UserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.save()
return redirect('user_detail', pk=user.pk)
else:
form = UserForm()
return render(request, 'user_edit.html', {'form': form})
def user_edit(request, pk):
user = get_object_or_404(User, pk=pk)
if request.method == "POST":
form = UserForm(request.POST, instance=user)
if form.is_valid():
user = form.save(commit=False)
user.save()
return redirect('user_detail', pk=user.pk)
else:
form = UserForm(instance=user)
return render(request, 'user_edit.html', {'form': form})
def user_delete(request, pk):
user = get_object_or_404(User, pk=pk)
user.delete()
return redirect('user_list')
```
7. 在Django应用的urls.py中,配置URL路由:
```
from django.urls import path
from . import views
urlpatterns = [
path('', views.user_list, name='user_list'),
path('user/<int:pk>/', views.user_detail, name='user_detail'),
path('user/new/', views.user_new, name='user_new'),
path('user/<int:pk>/edit/', views.user_edit, name='user_edit'),
path('user/<int:pk>/delete/', views.user_delete, name='user_delete'),
]
```
8. 在Django应用的templates目录下,编写HTML模板文件:
user_list.html:
```
{% extends "base.html" %}
{% block content %}
<h1>Users</h1>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Actions</th>
</tr>
{% for user in users %}
<tr>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.age }}</td>
<td>
<a href="{% url 'user_detail' pk=user.pk %}">View</a>
<a href="{% url 'user_edit' pk=user.pk %}">Edit</a>
<a href="{% url 'user_delete' pk=user.pk %}">Delete</a>
</td>
</tr>
{% endfor %}
</table>
<a href="{% url 'user_new' %}">Add User</a>
{% endblock %}
```
user_detail.html:
```
{% extends "base.html" %}
{% block content %}
<h1>{{ user.name }}</h1>
<p>Email: {{ user.email }}</p>
<p>Age: {{ user.age }}</p>
<a href="{% url 'user_edit' pk=user.pk %}">Edit</a>
<a href="{% url 'user_delete' pk=user.pk %}">Delete</a>
{% endblock %}
```
user_edit.html:
```
{% extends "base.html" %}
{% block content %}
<h1>{{ title }}</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>{% block title %}{% endblock %}</title>
</head>
<body>
<div>
<h1><a href="{% url 'user_list' %}">My Project</a></h1>
</div>
<div>
{% block content %}{% endblock %}
</div>
</body>
</html>
```
9. 在Django应用的forms.py中,定义表单类:
```
from django import forms
from .models import User
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('name', 'email', 'age',)
```
10. 运行Django开发服务器,查看效果:
```
python manage.py runserver
```
以上就是一个基于Python+Django+MySQL实现的Web数据库系统的示例代码。
阅读全文