django项目中为我的高血压网站用建一个layui后台系统,后台导航栏其中一项为药品信息管理页面对mysql库drug_db中的drug_interaction表(drug_id drug1 drug2 interaction)进行增删改查操作
时间: 2023-06-25 08:06:39 浏览: 159
django+mysql 药品管理系统
5星 · 资源好评率100%
您可以按照以下步骤来建立一个layui后台系统,并实现对drug_interaction表的增删改查操作:
1. 安装Django和mysqlclient
在命令行中输入以下命令:
```
pip install django
pip install mysqlclient
```
2. 创建Django项目和应用
在命令行中输入以下命令:
```
django-admin startproject hypertension
cd hypertension
python manage.py startapp drug
```
3. 配置数据库
在`settings.py`文件中配置数据库连接信息:
```python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'drug_db',
'USER': 'root',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
```
4. 创建模型
在`models.py`文件中创建DrugInteraction模型:
```python
from django.db import models
class DrugInteraction(models.Model):
drug_id = models.AutoField(primary_key=True)
drug1 = models.CharField(max_length=50)
drug2 = models.CharField(max_length=50)
interaction = models.CharField(max_length=100)
class Meta:
db_table = 'drug_interaction'
```
5. 生成数据库表
在命令行中输入以下命令:
```
python manage.py makemigrations
python manage.py migrate
```
6. 创建视图
在`views.py`文件中创建DrugInteractionView视图:
```python
from django.shortcuts import render
from django.http import JsonResponse
from .models import DrugInteraction
def index(request):
return render(request, 'index.html')
def drug_interaction_list(request):
drug_interactions = DrugInteraction.objects.all()
return JsonResponse({'data': list(drug_interactions.values())})
def drug_interaction_add(request):
drug1 = request.POST.get('drug1')
drug2 = request.POST.get('drug2')
interaction = request.POST.get('interaction')
drug_interaction = DrugInteraction(drug1=drug1, drug2=drug2, interaction=interaction)
drug_interaction.save()
return JsonResponse({'success': True})
def drug_interaction_edit(request):
drug_id = request.POST.get('drug_id')
drug1 = request.POST.get('drug1')
drug2 = request.POST.get('drug2')
interaction = request.POST.get('interaction')
drug_interaction = DrugInteraction.objects.get(drug_id=drug_id)
drug_interaction.drug1 = drug1
drug_interaction.drug2 = drug2
drug_interaction.interaction = interaction
drug_interaction.save()
return JsonResponse({'success': True})
def drug_interaction_delete(request):
drug_id = request.POST.get('drug_id')
drug_interaction = DrugInteraction.objects.get(drug_id=drug_id)
drug_interaction.delete()
return JsonResponse({'success': True})
```
7. 创建路由
在`urls.py`文件中创建路由:
```python
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('drug_interaction_list/', views.drug_interaction_list, name='drug_interaction_list'),
path('drug_interaction_add/', views.drug_interaction_add, name='drug_interaction_add'),
path('drug_interaction_edit/', views.drug_interaction_edit, name='drug_interaction_edit'),
path('drug_interaction_delete/', views.drug_interaction_delete, name='drug_interaction_delete'),
]
```
8. 创建模板
在`templates`文件夹中创建`index.html`模板:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>药品信息管理页面</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.css">
</head>
<body>
<div class="layui-container">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
<legend>药品信息管理页面</legend>
</fieldset>
<div class="layui-row">
<div class="layui-col-md12">
<table class="layui-hide" id="drug_interaction_table" lay-filter="drug_interaction_table"></table>
</div>
</div>
<div class="layui-row">
<div class="layui-col-md12">
<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="addDrugInteraction()">添加</button>
</div>
</div>
</div>
<script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.js"></script>
<script>
$(function () {
layui.use('table', function () {
var table = layui.table;
table.render({
elem: '#drug_interaction_table',
url: '/drug_interaction_list/',
page: true,
cols: [[
{field: 'drug_id', title: 'ID', width: 80},
{field: 'drug1', title: 'Drug1', width: 150},
{field: 'drug2', title: 'Drug2', width: 150},
{field: 'interaction', title: 'Interaction', width: 300},
{fixed: 'right', title: '操作', width: 150, align: 'center', toolbar: '#drug_interaction_toolbar'}
]]
});
table.on('tool(drug_interaction_table)', function (obj) {
var data = obj.data;
if (obj.event === 'edit') {
layer.prompt({
formType: 0,
value: data.interaction,
title: '修改交互关系'
}, function (value, index) {
$.ajax({
type: "POST",
url: "/drug_interaction_edit/",
data: {
'drug_id': data.drug_id,
'interaction': value
},
success: function (data) {
obj.update({
interaction: value
});
layer.close(index);
},
error: function () {
layer.msg('修改失败');
}
});
});
} else if (obj.event === 'delete') {
layer.confirm('确认删除该记录?', function (index) {
$.ajax({
type: "POST",
url: "/drug_interaction_delete/",
data: {
'drug_id': data.drug_id
},
success: function (data) {
obj.del();
layer.close(index);
},
error: function () {
layer.msg('删除失败');
}
});
});
}
});
});
});
function addDrugInteraction() {
layer.prompt({
formType: 0,
title: '添加交互关系'
}, function (value, index) {
$.ajax({
type: "POST",
url: "/drug_interaction_add/",
data: {
'drug1': 'drug1',
'drug2': 'drug2',
'interaction': value
},
success: function (data) {
window.location.reload();
},
error: function () {
layer.msg('添加失败');
}
});
});
}
</script>
<script type="text/html" id="drug_interaction_toolbar">
<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
</body>
</html>
```
9. 运行Django项目
在命令行中输入以下命令:
```
python manage.py runserver
```
然后访问`http://127.0.0.1:8000/`即可看到药品信息管理页面。在该页面中可以进行药品交互关系的增删改查操作。
阅读全文