用python 写一个图书管理系统 html+css+js django
时间: 2023-08-04 07:35:08 浏览: 172
基于Python+Django+Vue的在线图书借阅网站图书管理系统代码+数据库(高分毕设)
好的,以下是一个简单的图书管理系统的 Django 代码示例:
首先,在终端中创建一个 Django 项目:
```
django-admin startproject myproject
```
然后,创建一个名为 `myapp` 的应用程序:
```
cd myproject
python manage.py startapp myapp
```
接下来,我们需要安装一些第三方库,包括:
- Bootstrap:用于构建网页样式;
- jQuery:用于处理网页交互;
- django-crispy-forms:用于创建美观的表单。
可以通过以下命令安装这些库:
```
pip install django-bootstrap4 jquery django-crispy-forms
```
接下来,在 `myapp/models.py` 文件中定义一个 `Book` 模型:
```python
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
description = models.TextField(blank=True)
published_date = models.DateField(blank=True, null=True)
def __str__(self):
return self.title
```
在 `myapp/forms.py` 文件中创建一个 `BookForm` 表单:
```python
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'description', 'published_date']
```
在 `myapp/views.py` 文件中创建视图函数和路由:
```python
from django.shortcuts import render, redirect
from .models import Book
from .forms import BookForm
def book_list(request):
books = Book.objects.all()
return render(request, 'book_list.html', {'books': books})
def book_create(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
form.save()
return redirect('book_list')
else:
form = BookForm()
return render(request, 'book_form.html', {'form': form})
def book_update(request, pk):
book = Book.objects.get(pk=pk)
if request.method == 'POST':
form = BookForm(request.POST, instance=book)
if form.is_valid():
form.save()
return redirect('book_list')
else:
form = BookForm(instance=book)
return render(request, 'book_form.html', {'form': form})
def book_delete(request, pk):
book = Book.objects.get(pk=pk)
book.delete()
return redirect('book_list')
urlpatterns = [
path('', book_list, name='book_list'),
path('new/', book_create, name='book_create'),
path('<int:pk>/edit/', book_update, name='book_update'),
path('<int:pk>/delete/', book_delete, name='book_delete'),
]
```
在 `myapp/templates` 文件夹中创建三个 HTML 文件:
`base.html`:
```html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>{% block title %} {% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/main.css' %}">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="{% url 'book_list' %}">图书管理系统</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'book_list' %}">图书列表</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'book_create' %}">添加图书</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4">
{% block content %}
{% endblock %}
</div>
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
</body>
</html>
```
`book_list.html`:
```html
{% extends 'base.html' %}
{% block title %}图书列表{% endblock %}
{% block content %}
<h1>图书列表</h1>
<table class="table">
<thead>
<tr>
<th>标题</th>
<th>作者</th>
<th>描述</th>
<th>出版日期</th>
<th></th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ book.title }}</td>
<td>{{ book.author }}</td>
<td>{{ book.description }}</td>
<td>{{ book.published_date }}</td>
<td>
<a href="{% url 'book_update' book.pk %}" class="btn btn-sm btn-primary">编辑</a>
<a href="{% url 'book_delete' book.pk %}" class="btn btn-sm btn-danger">删除</a>
</td>
</tr>
{% empty %}
<tr>
<td colspan="5">暂无数据</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
```
`book_form.html`:
```html
{% extends 'base.html' %}
{% block title %}{{ form.instance.title }}{% endblock %}
{% block content %}
<h1>{{ form.instance.pk|default_if_none:"添加" }}图书</h1>
<form method="post">
{% csrf_token %}
<div class="form-group">
{{ form.title.label_tag }}
{{ form.title }}
</div>
<div class="form-group">
{{ form.author.label_tag }}
{{ form.author }}
</div>
<div class="form-group">
{{ form.description.label_tag }}
{{ form.description }}
</div>
<div class="form-group">
{{ form.published_date.label_tag }}
{{ form.published_date }}
</div>
<button type="submit" class="btn btn-primary">保存</button>
<a href="{% url 'book_list' %}" class="btn btn-secondary">取消</a>
</form>
{% endblock %}
```
最后,在 `myproject/settings.py` 文件中添加一些配置项:
```python
INSTALLED_APPS = [
'myapp',
'bootstrap4',
'crispy_forms',
]
CRISPY_TEMPLATE_PACK = 'bootstrap4'
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
```
现在可以启动 Django 开发服务器并访问 `http://127.0.0.1:8000/`,应该会看到一个简单的图书列表页面,可以添加、编辑和删除图书。
阅读全文