django項目 小示例圖書管理系統 -开发者知识库

django項目 小示例圖書管理系統 -开发者知识库,第1张

編寫models后進行表的初始化和生成

python manage.py makemigrations
python manage.py migrate


sqlite的驅動安裝http://blog.csdn.net/qq_36482772/article/details/53458400


拷貝bootstrap到static
setting最后追加 STATIC_ROOT=os.path.join(BASE_DIR,'book/static/bootstrap-3.3.7-dist')



urls.py文件內容

from django.conf.urls import url
from django.contrib import admin
from book import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^addBook/', views.addBook),
url(r'^delete/', views.delete),
url(r'^addBook/', views.addBook),
url(r'^edit/', views.edit),
]

views文件內容

from django.shortcuts import render, redirect
# from book import models
from book.models import *


# Create your views here.
def index(request):
all_book_list = Book.objects.all() # 取到的內容為[obj1,obj2,obj3]

return render(request, "index.html", {"all_book_list":all_book_list})


def addBook(request):
if request.method == 'POST':
title = request.POST.get('title')
price = request.POST.get('price')
author = request.POST.get('author')
publish = request.POST.get('publish')
print(request.POST)
# 數據庫添加操作
Book.objects.create(title=title, price=price, author=author, publish=publish)
return redirect("/index/")

return render(request, "addBook.html")
def delete(request,):
id=request.GET.get("id")
Book.objects.filter(id=id).delete()
return redirect("/index/")
def edit(request):
Book.objects.filter(id=id).update(title="title")
# Book.objects.filter()
return redirect("/index/")


modlels.py文件內容

from django.db import models


# Create your models here.

class Book(models.Model):
#id = models.IntegerField()
title = models.CharField(max_length=32) # 字符串
price=models.IntegerField()
author=models.CharField(max_length=32)
publish=models.CharField(max_length=32)

頁面內容

index

{% load staticfiles %}
{##新增內容#}

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.outer {
margin: 100px auto;
height: 400px;
{# background-color: greenyellow;#} border: 0px solid rebeccapurple;
width: 75%;
}
</style>
{# //引入外部文件#}
<link rel="stylesheet" href="{% static "dist/css/bootstrap.css" %}">
<script src="/img.php?pic={% static "dist/js/jquery-3.1.1.js" %}"></script>
<script src="/img.php?pic={% static "dist/js/bootstrap.js" %}"></script>
</head>
<body>

<div class="outer">

<a href="/addBook/">
<button type="button" class="btn btn-primary">添加</button>

<br/></a>
<hr/>
<table class="table table-striped">
<tr>
<th>編號</th>
<th>名字</th>
<th>價格</th>
<th>作者</th>
<th>出版社</th>
<th>管理</th>
</tr>
{% for book in all_book_list %}
<tr>
<td>{{ book.id }}</td>
<td>{{ book.title }}</td>
<td>{{ book.price }}</td>
<td>{{ book.author }}</td>
<td>{{ book.publish }}</td>
<td><a href="/delete/?id={{ book.id }}">
<button type="button" class="btn btn-danger">刪除</button>
</a>
<a href="/edit/?id={{ book.id }}">
<button type="button" class="btn btn-info">修改</button>
</a></td>
</tr>
{% endfor %}
{# {{ all_book_list }}#}
</table>
</div>
</body>
</html>

addbook

{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>

<link rel="stylesheet" href="{% static "dist/css/bootstrap.css" %}">
<script src="/img.php?pic={% static "dist/js/jquery-3.1.1.js" %}"></script>
<script src="/img.php?pic={% static "dist/js/bootstrap.js" %}"></script>
</head>
<style>
.addbook{
margin-top: 10%;
margin-right: 50%;
}
</style>
<body>
<br>
<div class="addbook">
<form class="form-horizontal" action="" method="post">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">名字</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" placeholder="title" name="title">
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">價格</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="price" placeholder="price" name="price">
</div>
</div>
<div class="form-group">
<label for="author" class="col-sm-2 control-label">作者</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="author" placeholder="author" name="author">
</div>
</div>
<div class="form-group">
<label for="publish" class="col-sm-2 control-label">出版社</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="publish" placeholder="publish" name="publish">
</div>
</div>
{# <div class="form-group">#}
{# <div class="col-sm-offset-2 col-sm-10">#}
{# <div class="checkbox">#}
{# <label>#}
{# <input type="checkbox"> Remember me#}
{# </label>#}
{# </div>#}
{# </div>#}
{# </div>#}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>


附加

setting追加

STATIC_ROOT=os.path.join(BASE_DIR,'book/static/dist')


為了引入bootstrap

boot下拷貝入bootstrap的dist文件夾  js下拷貝入jQuery


並且注釋47行 為了post請求 不被攔截

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复