1.pycharm创建django项目
2.app下models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=32)
price = models.DecimalField(max_digits=8, decimal_places=2)
date = models.DateField()
publish = models.ForeignKey('Publish',on_delete=models.DO_NOTHING)
authors = models.ManyToManyField('Author')
def __str__(self):
return self.title
class Publish(models.Model):
name = models.CharField(max_length=32)
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=32)
def __str__(self):
return self.name
3.执行命令。
makemigrations
migrate
createsuperuser
4.在admin.py注册
from django.contrib import admin
from .models import *
admin.site.register(Book)
admin.site.register(Publish)
admin.site.register(Author)
5.登录后台127.0.0.1/admin 添加一些数据
6.根项目urls.py下
from django.contrib import admin
from django.urls import path
from app.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('book/',book),
path('book/add/',add_book),
path('book/edit/<id>/',edit_book),
path('book/delete/<id>/',delete_book),
]
7.app下views.py
from django.shortcuts import render, redirect, HttpResponse
from .models import *
def book(request):
list = Book.objects.all()
return render(request, 'book.html', locals())
def add_book(request):
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
date = request.POST.get('date')
publish = request.POST.get('publish')
authors = request.POST.getlist('authors')
book_obj = Book.objects.create(title=title, price=price, date=date, publish_id=publish)
book_obj.authors.add(*authors)
return redirect('/book/')
publishs = Publish.objects.all()
authors = Author.objects.all()
return render(request, 'book_add.html', locals())
def edit_book(request, id):
book = Book.objects.filter(pk=id).first()
publishs = Publish.objects.all()
authors = Author.objects.all()
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
date = request.POST.get('date')
publish = request.POST.get('publish')
authors = request.POST.getlist('authors')
Book.objects.filter(pk=id).update(title=title, price=price, date=date, publish_id=publish)
book.authors.set(authors)
return redirect('/book/')
return render(request, 'book_edit.html', locals())
def delete_book(request, id):
Book.objects.filter(pk=id).delete()
return redirect('/book/')
8.templates下创建book.html,book_add.html,book_edit.html
#book.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="/book/add/"><button>添加书籍</button></a>
<table border="1">
<thead>
<tr>
<th>序号</th>
<th>书名</th>
<th>价格</th>
<th>日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
</tr>
</thead>
{% for book in list %}
<tr>
<td> {{ forloop.counter }}</td>
<td> {{ book.title }}</td>
<td> {{ book.price }}</td>
<td> {{ book.date|date:'Y/m/d' }}</td>
<td> {{ book.publish.name}}</td>
<td> {{ book.authors.all }}</td>
<td>
<a href="/book/edit/{{ book.pk }}/"><button>编辑</button></a>
<a href="/book/delete/{{ book.pk }}/"><button>删除</button></a>
</td>
</tr>
{% endfor %}
</table>
</body>
</html>
#book_add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>添加书籍</h3>
<form action="" method="post">
{% csrf_token %}
书名:<input type="text" name="title" id=""><br>
价格:<input type="text" name="price" id=""><br>
日期:<input type="date" name="date" id=""><br>
出版社:
<select name="publish" id="">
{% for publish in publishs %}
<option value="{{ publish.pk }}">{{ publish.name }}</option>
{% endfor %}
</select>
<br>
作者:
<select name="authors" id="" multiple>
{% for author in authors %}
<option value="{{ author.pk }}">{{ author.name }}</option>
{% endfor %}
</select>
<br>
<input type="submit" value="post">
</form>
</body>
</html>
#book_edit.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>编辑书籍</h3>
<form action="" method="post">
{% csrf_token %}
书名:<input type="text" name="title" value="{{ book.title }}"><br>
价格:<input type="text" name="price" value="{{ book.price }}"><br>
日期:<input type="date" name="date" value="{{ book.date|date:'Y-m-d' }}"><br>
出版社:
<select name="publish" id="">
{% for publish in publishs %}
{% if book.publish == publish %}
<option selected value="{{ publish.pk }}">{{ publish.name }}</option>
{% else %}
<option value="{{ publish.pk }}">{{ publish.name }}</option>
{% endif %}
{% endfor %}
</select>
<br>
作者:
<select name="authors" id="" multiple>
{% for author in authors %}
{% if author in book.authors.all %}
<option selected value="{{ author.pk }}">{{ author.name }}</option>
{% else %}
<option value="{{ author.pk }}">{{ author.name }}</option>
{% endif %}
{% endfor %}
</select>
<br>
<input type="submit" value="post">
</form>
</body>
</html>