宿舍信息管理python代码
时间: 2023-07-09 12:47:33 浏览: 153
宿舍管理信息系统源码
以下是一个使用Django框架实现宿舍信息管理的示例代码,供你参考:
1. 创建数据库
首先需要创建一个MySQL数据库,并创建宿舍信息表,可以使用以下SQL语句:
```sql
CREATE TABLE `dormitory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL,
`area` float NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
2. 创建Django项目和应用
使用Django的命令行工具创建一个新的项目和应用:
```bash
django-admin startproject dormitory_manager
cd dormitory_manager
python manage.py startapp dormitory
```
3. 定义宿舍信息模型
在应用的models.py文件中定义宿舍信息的模型:
```python
from django.db import models
class Dormitory(models.Model):
name = models.CharField(max_length=255)
location = models.CharField(max_length=255)
area = models.FloatField()
price = models.FloatField()
def __str__(self):
return self.name
```
4. 创建API视图函数
在应用的views.py文件中定义宿舍信息的增删查改API:
```python
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from .models import Dormitory
def dormitory_list(request):
dormitories = Dormitory.objects.all()
data = {'results': list(dormitories.values(
'id', 'name', 'location', 'area', 'price'))}
return JsonResponse(data)
def dormitory_detail(request, pk):
dormitory = get_object_or_404(Dormitory, pk=pk)
data = {'results': {'id': dormitory.id,
'name': dormitory.name,
'location': dormitory.location,
'area': dormitory.area,
'price': dormitory.price}}
return JsonResponse(data)
def dormitory_create(request):
name = request.POST.get('name')
location = request.POST.get('location')
area = request.POST.get('area')
price = request.POST.get('price')
dormitory = Dormitory(name=name, location=location,
area=area, price=price)
dormitory.save()
data = {'results': {'id': dormitory.id,
'name': dormitory.name,
'location': dormitory.location,
'area': dormitory.area,
'price': dormitory.price}}
return JsonResponse(data)
def dormitory_update(request, pk):
dormitory = get_object_or_404(Dormitory, pk=pk)
dormitory.name = request.POST.get('name')
dormitory.location = request.POST.get('location')
dormitory.area = request.POST.get('area')
dormitory.price = request.POST.get('price')
dormitory.save()
data = {'results': {'id': dormitory.id,
'name': dormitory.name,
'location': dormitory.location,
'area': dormitory.area,
'price': dormitory.price}}
return JsonResponse(data)
def dormitory_delete(request, pk):
dormitory = get_object_or_404(Dormitory, pk=pk)
dormitory.delete()
data = {'results': {'message': 'Dormitory deleted successfully'}}
return JsonResponse(data)
```
5. 配置URL路由
在应用的urls.py文件中配置宿舍信息的URL路由:
```python
from django.urls import path
from .views import dormitory_list, dormitory_detail, dormitory_create, dormitory_update, dormitory_delete
urlpatterns = [
path('dormitories/', dormitory_list, name='dormitory_list'),
path('dormitories/<int:pk>/', dormitory_detail, name='dormitory_detail'),
path('dormitories/create/', dormitory_create, name='dormitory_create'),
path('dormitories/<int:pk>/update/', dormitory_update, name='dormitory_update'),
path('dormitories/<int:pk>/delete/', dormitory_delete, name='dormitory_delete'),
]
```
6. 创建前端页面
可以使用HTML和JavaScript等前端技术,创建宿舍信息管理的前端页面,其中通过AJAX调用API实现增删查改功能。
以上是一个使用Django框架实现宿舍信息管理的示例代码,具体实现过程可能会有所不同,需要根据具体情况进行调整和完善。
阅读全文