urls:

from django.contrib import admin
from django.urls import path,re_path
from first import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',views.index, name='index'), #显示的主界面
# path('one/',views.one)
path('add/',views.add,name='add'), #添加界面
# re_path('del_me/(?P<id>\d+)',views.del_message,name = 'edit_book'),
re_path('del_book/',views.del_message,name='d_book'), #删除
re_path('edit_book/(\d+)/',views.edit_book,name ='edit_book'), #编辑
re_path('login/',views.login,name='login'), #登陆
path('logout/',views.logout, name='logout'), #注销
]

urls

views:

from django.shortcuts import render,redirect,HttpResponse,reverse
from first.models import Book,Publish,Author,Gfriend,User
import json
import datetime
# Create your views here. # 等一一个装饰器来验证是否可以登陆后进行操作 def required_login(func):
def inner(*args, **kwargs):
request = args[0] # 因为你的参数实在args内 所以你的接收的内容也是args
if request.COOKIES.get('is_login'):
return func(*args, **kwargs) else:
return redirect(reverse('login')) return inner @required_login def index(request):
book_obj = Book.objects.all()
author_obj = Author.objects.all()
publish_obj = Publish.objects.all()
# book_author = Book.authors.all() # print(book_obj)
# print(1111)
return render(request, 'index.html',locals()) @required_login
def add(request):
author_obj = Author.objects.all()
publish_obj = Publish.objects.all()
if request.method =="POST":
name = request.POST.get('name')
price = request.POST.get('price')
dat = request.POST.get('datetime')
publish = request.POST.get('publish')
author_list =request.POST.getlist('authors') #获取的作者是多个就需要用getlist来获取
print(author_list)
book = Book.objects.create(name = name,price = price,date = dat,publish_id = publish )
# book.publish_id.add(publish)
book.authors.set(author_list) #用set依次添加
return redirect('/index/')
# ut = reverse('add')
return render(request,'add.html',locals()) @required_login
def del_message(request):
del_id = request.POST.get('del_id')
# Book.objects.filter(id = del_id).delete() del_book = Book.objects.filter(id = del_id)
# print(del_book)
del_book.delete()
# return redirect('/index/'
#删除成功过后你需要进行发送一个信息 不能重定向了 因为用的是ajax局部刷新的
return HttpResponse(json.dumps({'status':1})) #返送一个序列化字符串 @required_login
def edit_book(request,edid):
book_obj = Book.objects.get(id = edid)
author_obj = Author.objects.all()
publish_obj = Publish.objects.all() if request.method == 'POST':
name = request.POST.get('name')
price = request.POST.get('price') #get这个值是获取你前端界面的name的属性 value的属性是显示的属性
date = request.POST.get('datetime')
publish = request.POST.get('publish')
authors = request.POST.getlist('authors')
book_obj.name = name
book_obj.price = price
book_obj.date = date
book_obj.publish_id = publish
book_obj.authors.set(authors)
book_obj.save()
return redirect('/index/')
return render(request,'edit_book.html',locals()) # @required_login
def login(request):
if request.method == 'POST':
name = request.POST.get('user')
pwd = request.POST.get('pwd')
user_obj = User.objects.filter(name = name,pwd=pwd)
print(user_obj)
user_obj = user_obj.first() #因为你取到的user_obj是一个queryset所以你要进行取到第一个是对象 if user_obj: #登陆成功
# 然后用cookie
ret = redirect(reverse('index')) #登陆成功后跳转到index界面 这个用的反向解析 ret.set_cookie('name',name)
ret.set_cookie('pwd',pwd)
ret.set_cookie('is_login',True)
#然后你求登陆时间 选哟调用你你数据库中的上一次登陆成功后存储的时间
ret.set_cookie('last_time',user_obj.date)
user_obj.date = datetime.datetime.now() #把你这一次登陆的时间再设置回数据库
user_obj.save()
return ret
return render(request,'login.html') @required_login
def logout(request):
ret = redirect(reverse('login')) #退还到你的登陆框
ret.delete_cookie('is_login')
ret.delete_cookie('user')
ret.delete_cookie('last_time')
return ret

views

models:(数据库):

from django.db import models

# Create your models here.

class Book(models.Model):
name = models.CharField(max_length = 30)
price = models.DecimalField(max_digits=30,decimal_places=3)
date = models.DateTimeField()
publish = models.ForeignKey(to="Publish",to_field='id',on_delete = models.CASCADE)
authors = models.ManyToManyField(to='Author') class Publish(models.Model):
title = models.CharField(max_length = 30)
addr = models.CharField(max_length = 30) class Author(models.Model):
name = models.CharField(max_length = 30)
addr = models.CharField(max_length = 30)
gf = models.OneToOneField(to='Gfriend',to_field='id',on_delete=models.CASCADE) class Gfriend(models.Model):
name = models.CharField(max_length =30)
age = models.IntegerField() class User(models.Model):
name = models.CharField(max_length = 30)
pwd = models.IntegerField()
date = models.DateTimeField()

models

template:(前端界面)

index(显示的主界面):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> </head>
<body> <div class="bs-example " data-example-id="inverted-navbar ">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header ">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
data-target="#bs-example-navbar-collapse-9" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">图书管理</a>
</div> <!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse pull-right" id="bs-example-navbar-collapse-9">
<ul class="nav navbar-nav">
<li class="active"><a href="#">{{ request.COOKIES.name }}</a></li>
<li><a href="#">{{ request.COOKIES.last_time}}</a></li>
<li class ='btn-primary'><a href="{% url 'logout' %}">注销</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div> <table class="table table-hover">
<thead>
<a href="{% url 'add' %}" class="btn btn-primary ">添加</a>
<tr>
<th>序号</th>
<th>名字</th>
<th>价格</th>
<th>出版日期</th>
<th>出版社</th>
<th>作者</th>
<th>操作</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for foo in book_obj %}
<tr>
<th scope="row">{{ forloop.counter }}</th>
{# {{ forloop.count 是自动为它排序的 }}#}
<td>{{ foo.name }}</td>
<td>{{ foo.price }}</td>
<td>{{ foo.date|date:'Y-m-d' }}</td>
<td>{{ foo.publish.title }}</td>
<td>
{% for i in foo.authors.all %}
{# {% for i in book_author %}#} {% if not forloop.last %}
{{ i.name }}{{ '|' }}
{% else %}
{{ i.name }}
{% endif %}
{% endfor %}
</td>
<td><a href="{% url 'edit_book' foo.id %}" class="tianjia btn btn-warning">编辑</a></td>
<td><a class="delet btn btn-danger" edit_id="{{ foo.id }}">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> {% csrf_token %}
<script>
$('.btn-danger').click(function () {
_this = $(this)
{#alert(_this.attr('edit_id'));#}
$.ajax({
url:{% url 'd_book' %},
type: 'post',
data: {
del_id: _this.attr('edit_id'),
csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val()
},
success: function (data) {
console.log(data);
var data = JSON.parse(data);
{# 把接收过来的信息反序列化#}
if (data.status) {
_this.parent().parent().remove()
} }
})
}) </script> </body>
</html>

index

add(添加书籍):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
<style>
.conten {
margin-top: 50px;
}
</style>
</head>
<body>
<div class="conten">
<div>
<div class="container">
<div class="bs-example" data-example-id="form-validation-states"> <form method="post" action="{% url 'add' %}"> {% csrf_token %}
<div class="form-group has-success"> <label class="control-label" for="control-label">书籍名称</label>
<input type="text" class="form-control" id="control-label" aria-describedby="helpBlock2"
name="name">
</div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning">价格</label>
<input type="text" class="form-control" id="inputWarning" name="price">
</div>
<div class="form-group has-error">
<label class="control-label" for="inputError1">出版日期</label>
<input type="date" class="form-control" id="inputError1" name="datetime">
</div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning1">出版社</label>
{# <input type="text" class="form-control" id="inputWarning1" name="publish">#}
<select name="publish" id="inputWarning1" class=" form-control">
{% for foo in publish_obj %}
<option value="{{ foo.id }}">{{ foo.title }}</option>
{% endfor %} </select>
</div> <div class="form-group has-success">
<label class="control-label" for="inputSuccess1">作者</label>
{# <input type="text" class="form-control" id="inputSuccess1" aria-describedby="helpBlock2" name="authors" multiple>#}
<select name="authors" id="inputSuccess1" class="form-control" multiple>
{% for f in author_obj %}
<option value="{{ f.id }}">{{ f.name }}</option>
{% endfor %}
</select>
</div> <input type="submit" value="提交"/> </form>
</div>
</div>
</div>
</div> </body>
</html>

add

编辑:(edit_book)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
<style>
.conten {
margin-top: 50px;
}
</style>
</head>
<body>
<div class="conten">
<div>
<div class="container">
<div class="bs-example" data-example-id="form-validation-states"> <form method="post" action=""> {% csrf_token %}
<div class="form-group has-success"> <label class="control-label" for="control-label">书籍名称</label>
<input type="text" class="form-control" id="control-label" aria-describedby="helpBlock2"
name="name" value={{ book_obj.name }}>
</div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning">价格</label>
<input type="text" class="form-control" id="inputWarning" name="price" value ={{ book_obj.price }}>
</div>
<div class="form-group has-error">
<label class="control-label" for="inputError1">出版日期</label>
<input type="date" class="form-control" id="inputError1" name="datetime" value={{ book_obj.date|date:'Y-m-d' }}>
</div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning1">出版社</label>
{# <input type="text" class="form-control" id="inputWarning1" name="publish">#}
<select name="publish" id="inputWarning1" class=" form-control">
{% for foo in publish_obj %}
{{% if book_obj.publish == foo %}
<option value="{{ foo.id }}" selected>{{ foo.title }}</option>
{% else %}
<option value="{{ foo.id }}">{{ foo.title }}</option> {% endif %}}
{# <option value="{{ foo.id }}">{{ foo.title }}</option>#}
{% endfor %} </select>
</div> <div class="form-group has-success">
<label class="control-label" for="inputSuccess1">作者</label>
{# <input type="text" class="form-control" id="inputSuccess1" aria-describedby="helpBlock2" name="authors" multiple>#}
<select name="authors" id="inputSuccess1" class="form-control" multiple>
{% for f in author_obj %}
{% if f in book_obj.authors.all %}
<option value="{{ f.id }}" selected>{{ f.name }}</option>
{# selected是默认选中的意思#}
{% else %}
<option value="{{ f.id }}">{{ f.name }}</option> {% endif %}
{# <option value="{{ f.id }}">{{ f.name }}</option>#}
{% endfor %}
</select>
</div> <input type="submit" value="提交"/> </form>
</div>
</div>
</div>
</div> </body>
</html>

edit_book

登陆界面:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> <style>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
} .form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
} </style>
</head>
<body> <div class="container"> <form class="form-signin" action="{% url 'login' %}" method="post">
{% csrf_token %}
<h2 class="form-signin-heading">Please 登陆</h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="text" id="inputEmail" class="form-control" placeholder="Email address" required="" autofocus="" name="user">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required="" name="pwd">
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form> </div> <!-- /container --> <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
{# <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>#} </body>
</html>

login

记得不要忘记配置static你所引入的前端样式  还有settings内的数据库配置:

settings:

配置数据库:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'test', # 要连接的数据库,连接前需要创建好
'USER':'root', # 连接数据库的用户名
'PASSWORD':'zhaoyun', # 连接数据库的密码
'HOST':'127.0.0.1', # 连接主机,默认本级
'PORT':3306 # 端口 默认3306
}
} 配置static: STATICFILES = [
os.path.join(BASE_DIR,'static')
]

书籍管理系统 -----没有form组件的更多相关文章

  1. 图书管理系统 基于form组件

    models: from django.db import models # Create your models here. class Book(models.Model): name = mod ...

  2. Django——form组件和ModelForm

    一.原生form实现书城增删改查 1.构建模型并完成数据库迁移 (1)构建书城模型 from django.db import models # Create your models here. # ...

  3. 14 Django之Form和Model Form组件

    一.什么是Form 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用 ...

  4. 9.24 Django Form组件

    2018-9-23 20:10:04 这两天优化了自己图书管理系统 github 连接:https://github.com/TrueNewBee/pythonDemo 顺便整理了博客,写了好多总结, ...

  5. Django的form组件

    forms组件 forms组件,是一个类.在视图函数中创建一个类,类需要继承forms.Form from django import  forms 1.校验数据 步骤和语法: 1. 创建一个form ...

  6. jsp-servlet(2)响应HTML文档-书籍管理系统

    基础知识预备:  目标: 构建一个书籍管理系统,实现以下功能. 功能: 1 图书信息查询,(查) 2 书籍管理:添加书籍 3 书籍管理:修改书籍信息 4 书籍管理:删除书籍 一.预备工作 Book{ ...

  7. day85 ModuleForm Form组件

    1 forms组件与modelform组件 forms组件: https://www.cnblogs.com/yuanchenqi/articles/9036474.htmlmodelForm组件:h ...

  8. Django 的 model form 组件

    Django 的 model form 组件 Model Form 组件的由来 之前介绍过 Django 的 Form 组件(Django的Form表单)使用方法,Form 组件能够帮我们做三件事: ...

  9. form组件之modelForm

    modelForm的使用及参数设置 从modelForm这个名字就能看出来,这个form是和模型类model有知己诶关联的,还是以数和出版社的模型来说明: models.py(模型) from dja ...

随机推荐

  1. hibernate关联关系的crud2

    hibernate关联关系的CRUD操作,解释都在注释里了,讲了fetchType.cascade. User类: package com.oracle.hibernate; import javax ...

  2. 使用单体模式设计原生js插件

    ----------基于上次写的jquery插件进行改造  http://www.cnblogs.com/GerryOfZhong/p/5533773.html 背景:jQuery插件依赖jQuery ...

  3. PHP之mb_convert_variables使用

    mb_convert_variables (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_convert_variables - Convert character code ...

  4. IDEA 启动项目,tomcat中配置的虚拟路径无法使用

    有时候,使用idea启动项目,非动静分离项目,直接根据图片url地址显示图片,会发现图片无法显示,tomcat中配置的虚拟路径无法使用,这时候需要配置idea.选择路径,然后给与一个访问名就行了.

  5. spring mongodb中去掉_class列

    调用mongoTemplate的save方法时, spring-data-mongodb的TypeConverter会自动给document添加一个_class属性, 值是你保存的类名. 这种设计并没 ...

  6. mysql 索引数据结构及原理

    原文:http://www.uml.org.cn/sjjm/201107145.asp 1 索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子 ...

  7. [Mysql 查询语句]——查询字段

    查询所有字段     select  *  from  表名; 可以用 * 号代表所有字段 select * from vendors; +---------+----------------+--- ...

  8. Cannot find module 'rxjs/operators/share'

    描述: ionic项目,在使用了ngx-translate之后,项目编译完成,运行到浏览器时,出现如下错误: 其中ngx-translate参照官网最新教程使用,并且也尝试了angular4.3之前的 ...

  9. ios开发 学习积累20161027~20161031

    前言 学习ios这几天来,总结下,函数的定义,调用.跟其他语言都有一定的区别: 几个特别重要的就是对象的迭代的使用和判断.取随机数.动画的实现及数组的深入研究等等 之前的总结地址 ios开发 学习积累 ...

  10. php and js to facebook登陆 最佳实践

    Facebook Login Flow & Best Practices Best practice for Facebook login flow with the JavaScript S ...