当前位置 主页 > 服务器问题 > Linux/apache问题 >
本文实例讲述了django框架单表操作之增删改。分享给大家供大家参考,具体如下:
首先找到操作的首页面
代码如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" rel="external nofollow" > <title>书列表</title> </head> <body> <div class="container"> <a href="/add_book/" rel="external nofollow" class="btn btn-success">添加新书</a> <div class="panel panel-primary"> <div class="panel-heading">书籍管理</div> <div class="panel-body"> <table class="table table-bordered table-striped"> <thead> <tr> <th>#</th> <th>书名</th> <th>操作</th> </tr> </thead> <tbody> {% for book in book_list %} <tr data-> <td>{{ forloop.counter }}</td> <td>{{ book.title }}</td> <td><a href="/delete_book/?id={{ book.id }}" rel="external nofollow" class="btn btn-danger">删除</a> <a href="/edit_book/?id={{ book.id }}" rel="external nofollow" class="btn btn-info">修改</a></td> 此处的?id可以改成 ?iid,或者其他的名称,在views.py文件里对函数edit_book修改即可edit_id=request.GET.get('iid') </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
主页:
之后,根据不同的操作指向不同的页面,这部分功能需要修改urls.py
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ # url(r'^admin/', admin.site.urls), url(r'^home/',views.home), url(r'^index/',views.index), url(r'^login/',views.login), url(r'^book_list/',views.book_list), #添加新书 url('^add_book/',views.add_book), #删除书籍 url('^delete_book/',views.delete_book), #修改书籍 url(r'^edit_book/',views.edit_book), ]
其次,不同操作指向不同的页面
add_book.html
主要的部分
<form class="form-horizontal" action="/add_book/" method="post"> #提交到 add_book <div class="form-group"> <label for="inputbookname" class="col-sm-2 control-label">书籍名称</label> <div class="col-sm-3"> <input type="text" class="form-control" name="book_name"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">添加新书</button>
edit_book.html
主要部分
<form class="form-horizontal" action="/edit_book/" method="post"> <input hidden type="text" name="book_id" value="{{ book.id }}"> <div class="form-group"> <label for="inputbookname" class="col-sm-2 control-label">书籍名称</label> <div class="col-sm-3"> <input type="text" class="form-control" name="book_name" value="{{ book.title }}"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交修改</button>