您的位置:首页 > 编程语言 > Go语言

django项目 小示例 图书管理系统

2017-08-20 00:48 435 查看
编写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="{% static "dist/js/jquery-3.1.1.js" %}"></script>
<script src="{% 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="{% static "dist/js/jquery-3.1.1.js" %}"></script>
<script src="{% 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请求 不被拦截
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: