使用pycharm,利用django3+python写一个宿舍和办公室物品管理系统代码,宿舍包括房间号,床,椅子,办公室包括房间号,办公桌,电脑
时间: 2024-03-11 18:51:50 浏览: 70
以下是一个简单的宿舍和办公室物品管理系统的代码示例,包括宿舍房间号、床、椅子和办公室房间号、办公桌、电脑的模型定义、视图函数、模板文件和 URL 路由配置。请注意,这个示例只是一个简单的演示,实际项目需要根据具体需求进行修改和完善。
1. models.py
```python
from django.db import models
# 定义宿舍房间号、床、椅子和办公室房间号、办公桌、电脑的模型
class DormRoom(models.Model):
room_number = models.CharField(max_length=20)
class DormBed(models.Model):
room = models.ForeignKey(DormRoom, on_delete=models.CASCADE)
bed_number = models.CharField(max_length=20)
class DormChair(models.Model):
room = models.ForeignKey(DormRoom, on_delete=models.CASCADE)
chair_number = models.CharField(max_length=20)
class OfficeRoom(models.Model):
room_number = models.CharField(max_length=20)
class OfficeDesk(models.Model):
room = models.ForeignKey(OfficeRoom, on_delete=models.CASCADE)
desk_number = models.CharField(max_length=20)
class OfficeComputer(models.Model):
room = models.ForeignKey(OfficeRoom, on_delete=models.CASCADE)
computer_number = models.CharField(max_length=20)
```
2. views.py
```python
from django.shortcuts import render
from .models import DormRoom, DormBed, DormChair, OfficeRoom, OfficeDesk, OfficeComputer
# 定义视图函数,查询并显示宿舍和办公室的信息
def room_list(request):
dorm_rooms = DormRoom.objects.all()
dorm_beds = DormBed.objects.all()
dorm_chairs = DormChair.objects.all()
office_rooms = OfficeRoom.objects.all()
office_desks = OfficeDesk.objects.all()
office_computers = OfficeComputer.objects.all()
context = {'dorm_rooms': dorm_rooms, 'dorm_beds': dorm_beds, 'dorm_chairs': dorm_chairs, 'office_rooms': office_rooms, 'office_desks': office_desks, 'office_computers': office_computers}
return render(request, 'room_list.html', context)
```
3. templates/room_list.html
```html
<!DOCTYPE html>
<html>
<head>
<title>宿舍和办公室物品管理系统</title>
</head>
<body>
<h1>宿舍房间号</h1>
<ul>
{% for dorm_room in dorm_rooms %}
<li>{{ dorm_room.room_number }}</li>
{% endfor %}
</ul>
<h1>床</h1>
<ul>
{% for dorm_bed in dorm_beds %}
<li>{{ dorm_bed.bed_number }} ({{ dorm_bed.room.room_number }})</li>
{% endfor %}
</ul>
<h1>椅子</h1>
<ul>
{% for dorm_chair in dorm_chairs %}
<li>{{ dorm_chair.chair_number }} ({{ dorm_chair.room.room_number }})</li>
{% endfor %}
</ul>
<h1>办公室房间号</h1>
<ul>
{% for office_room in office_rooms %}
<li>{{ office_room.room_number }}</li>
{% endfor %}
</ul>
<h1>办公桌</h1>
<ul>
{% for office_desk in office_desks %}
<li>{{ office_desk.desk_number }} ({{ office_desk.room.room_number }})</li>
{% endfor %}
</ul>
<h1>电脑</h1>
<ul>
{% for office_computer in office_computers %}
<li>{{ office_computer.computer_number }} ({{ office_computer.room.room_number }})</li>
{% endfor %}
</ul>
</body>
</html>
```
4. urls.py
```python
from django.urls import path
from . import views
# 配置 URL 路由
urlpatterns = [
path('', views.room_list, name='room_list'),
]
```
这个示例实现了一个简单的宿舍和办公室物品管理系统,可以显示宿舍房间号、床、椅子和办公室房间号、办公桌、电脑的信息。这只是一个简单的示例,实际项目需要根据具体需求进行修改和完善。
阅读全文