圖書管理系統(二):圖書列表的增長、刪除和編輯

  • 步驟一:建立圖書列表
  • 步驟二:增長圖書
  • 步驟三:刪除圖書
  • 步驟四:編輯圖書
 
步驟一:建立圖書列表

1.建立數據庫Book:
#app_publisher/models.py
 
class Book(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=64, null=False, unique=True)
    publisher = models.ForeignKey(to='publisher')
    #因爲ForeignKey會自動把publisher屬性後面加_id,也就是數據庫中是publisher_id,所以在這裏把_id給去掉了
python3 manage.py makemigrations    #用小本本將models.py裏面的改動記錄下來,記錄到app1/migrations文件夾下面
python3 manage.py migrate    #把改動翻譯成SQL語句去數據庫執行
2.寫入函數:
url(r'^book_list/', views.book_list),
def book_list(request):
    data = models.Book.objects.all()
    return render(request, 'book_list.html', {'data': data})
3.寫book_list.html文件:
<!DOCTYPE html>
<html>
<head>
    <title>圖書列表</title>
</head>
<body>
 
<table border='1'>
    <tr>
        <th>序號</th>
        <th>id</th>
        <th>title</th>
        <th>publisher</th>
    </tr>
    {% for i in data %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ i.id }}</td>
            <td>{{ i.title }}</td>
            <td>{{ i.publisher.name }}</td>
        </tr>
    {% endfor %}
</table>
 
</body>
</html>
 
 
步驟二:增長圖書

1.寫入函數:
url(r'^add_book/', views.add_book),
def add_book(request):
    if request.method == 'POST':
        add_name = request.POST.get('add_name')
        new_publisher = request.POST.get('publisher')
        models.Book.objects.create(title=add_name, publisher_id=new_publisher).save()
        return redirect('/book_list/')
    data = models.publisher.objects.all()
    return render(request, 'add_book.html', {'data': data})
2.寫html文件:
<a href="/add_book/">添加新圖書</a>
<!DOCTYPE html>
<html>
<head>
    <title>增長圖書</title>
</head>
<body>
    <h1>增長圖書</h1>
    <form action="/add_book/" method="post">
        <input type="text" name="add_name">
 
        出版社:
        <select name='publisher'>
        {% for i in data %}
            <option value="{{ i.id }}">{{ i.name }}</option>
        {% endfor %}
        </select>
 
        <input type="submit" value="提交">
    </form>
</body>
</html>
 
步驟三:刪除圖書

1.寫入函數:
url(r'^delete_book/', views.delete_book),
def delete_book(request):
    ret = request.GET.get('id')
    models.Book.objects.get(id=ret).delete()
    return redirect('/book_list/')
2.在book_list.html中增長刪除功能:
    {% for i in data %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ i.id }}</td>
            <td>{{ i.title }}</td>
            <td>{{ i.publisher.name }}</td>
             <td><a href="/delete_book/?id={{ i.id }}">刪除</a></td>
        </tr>
    {% endfor %}
 
步驟四:編輯圖書

1.寫入函數:
url(r'^edit_book/', views.edit_book),
def edit_book(request):
    if request.method == 'POST':
        edit_id = request.POST.get('edit_id')
        edit_name = request.POST.get('edit_title')
        print(edit_name)
        edit_publisher = request.POST.get('edit_publisher')
        ret = models.Book.objects.get(id=edit_id)
        ret.title = edit_name
        ret.publisher_id = edit_publisher
        ret.save()
        return redirect('/book_list/')
    edit_id = request.GET.get('id')
    ret = models.Book.objects.get(id=edit_id)
    datas = models.publisher.objects.all()
    return render(request, 'edit_book.html', {'datas': datas, 'ret':ret})
2.寫html文件:
<td><a href="/edit_book/?id={{ i.id }}">編輯</a></td>
<!DOCTYPE html>
<html>
<head>
    <title>修改圖書</title>
</head>
<body>
 
<h1>修改圖書</h1>
<form action="/edit_book/" method="post">
    <input type="text" name="edit_id" value="{{ ret.id }}" style="display: none">
    <input type="text" name='edit_title' value="{{ret.title}}">
 
    <select name="edit_publisher">
        {% for data in datas %}
            {% if ret.publisher_id == data.id %}
                <option selected value="{{ data.id }}">{{ data.name }}</option>
            {% else %}
                <option value="{{ data.id }}">{{ data.name }}</option>
            {% endif %}
        {% endfor %}
    </select>
 
    <input type="submit" value="提交">
 
</form>
</body>
</html>
相關文章
相關標籤/搜索