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()
email = models.EmailField()

models

settings配置:

配置数据库

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

init:

import pymysql
pymysql.install_as_MySQLdb()

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('add_book/',views.add_book,name='add_book'), re_path('del_book/',views.del_book ,name='del_book'), re_path('edit_book/(\d+)/',views.edit_book,name = 'edit_book'), path('login/',views.login_session,name='login'), path('logout/',views.logout,name = 'logout'),
path('register/',views.register, name = 'register'),
path('hint/',views.hint,name = 'hint') #这个是你的 跳转注册的界面 ]

urls

views:

from django.shortcuts import render,redirect,reverse,HttpResponse
import json,datetime from first.models import Book,Author,Publish,User from first .form import U_ser
# Create your views here. #定义一个装饰器 然后所有的 函数都用装饰器进行封装
def wrapper(func):
def inner(*args,**kwargs):
request = args[0]
# if request.COOKIES.get('is_login'):
if request.session.get('is_login'): return func(*args,**kwargs)
else:
return redirect(reverse('login')) return inner #登陆函数 cookie
# def login(request):
# print('11111111111111111')
# if request.method == 'POST':
# name = request.POST.get('user')
# pwd = request.POST.get('pwd')
# print(name,pwd)
# log_obj = User.objects.filter(name = name ,pwd=pwd).first()
# if log_obj: #登陆成功
# ret = redirect(reverse('index'))
# ret.set_cookie('name',name)
# ret.set_cookie('pwd',pwd)
# ret.set_cookie('is_login',True)
# ret.set_cookie('last_time',log_obj.date)
# log_obj.date = datetime.datetime.now()
# log_obj.save()
#
# return ret
# # return redirect(reverse('login'))
# return render(request,'login.html') @wrapper
#主界面
def index(request):
book_list = Book.objects.all()
publish_list = Publish.objects.all()
author_list = Author.objects.all() return render(request,'index.html',locals()) @wrapper
def add_book(request):
'''
这是对书籍的信息进行增加的函数
:param request:
:return:
'''
book_list = Book.objects.all()
publish_list = Publish.objects.all()
author_list = Author.objects.all() if request.method == 'POST':
name = request.POST.get('name')
price = request.POST.get('price')
date = request.POST.get('date')
publish = request.POST.get('publish')
author = request.POST.getlist('author')
print(author)
book = Book.objects.create(name = name,price = price, date = date ,publish_id=publish)
book .authors.set(author) #给你的这个书籍对象 添加作者 return redirect(reverse('index')) #添加完之后就重新定向到主界面 return render(request,'add_book.html',locals()) @wrapper
def del_book(request): #删除函数
deid = request.POST.get('deid')
print(deid)
del_book = Book.objects.filter(id = deid) #获取你前端点击的那个id的书籍对象
del_book.delete()
print(111*100)
return HttpResponse(json.dumps({'status':1})) @wrapper
def edit_book(request ,edid):
# print('___________________________')
book_list = Book.objects.filter(id=edid).first()
publish_list = Publish.objects.all()
author_list = Author.objects.all()
print(book_list)
print(6666666666666)
if request.method =='POST':
print(2222222222) name = request.POST.get('name')
price = request.POST.get('price')
date = request.POST.get('date')
publish = request.POST.get('publish')
author = request.POST.getlist('author')
# bk_bj =Book.objects.filter(id=edid).update(name=name, price=price, date=date, publish=publish)
# bk_bj.author.set(author)
print(3333333333333)
book_list.name = name
book_list.price = price
book_list.publish_id = publish
book_list.authors.set(author)
book_list.save()
return redirect(reverse('index'))
print(55555555555555)
return render(request,'edit_book.html',locals()) @wrapper
def logout(request): #注销
ret = redirect(reverse('login'))
# ret.delete_cookie('name')
# ret.set_cookie('last_time')
# ret.delete_cookie('pwd')
request.session.flush()
return ret #这个使用session
def login_session(request):
print(request.POST)
if request.method == 'POST':
name = request.POST.get('user','') pwd = request.POST.get('pwd','') user_obj = User.objects.filter(name=name ,pwd =pwd).first() if user_obj:
ret = redirect(reverse('index'))
request.session['is_login'] = True
request.session['name'] = name
request.session['pwd'] = pwd
request.session['last_time'] = str(user_obj.date) # 你去出的是一个date兑现 但是要存起来就要用 str
user_obj.date = datetime.datetime.now()
user_obj.save()
return ret
# else: #这一步是你输入的账号和密码不对的时候
# return HttpResponse('您输入用户名不对')
return redirect(reverse('hint'))
return render(request,'login.html') #定义一个注册用户的函数
def register(request): if request.method == 'POST': form = U_ser(request.POST) #把前端的信息和form组件对比判断
if form.is_valid():#正确
name = request.POST.get('name')
pwd = request.POST.get('pwd')
date = request.POST.get('date')
email = request.POST.get('email')
User.objects.create(name = name ,pwd = pwd,date = date,email = email) return redirect(reverse('login')) #注册成功就去登陆界面
else: g_error = form.errors.get('__all__') #获取所有的错误
if g_error:
g_error = g_error[0]
print(locals())
return render(request,'register.html',locals()) form = U_ser()
print(55555555555555)
return render(request,'register.html',locals()) def hint(request):
return render(request,'hint.html')

views

templates:

登陆界面:login

<!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' novalidate>
{% csrf_token %}
<h2 class="form-signin-heading">Please </h2>
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" 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>
{# <button class="btn btn-lg btn-info btn-block" type="submit">Register</button>#}
<a href="{% url 'register' %}" class="btn btn-lg btn-info btn-block" >Register</a>
</form> </div> </body>
</html>

login

注册:register

<!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>
.container {
margin-top: 80px; } {#body {#}
{# background-color: darkslategray;#}
.nide{
margin-top: 10px;
}
</style>
</head>
<body> {#<div class="container col-md-3 col-md-offset-4">#}
{# <div>#}
{# <div>#}
{# <div class="bs-example" data-example-id="form-validation-states">#}
{# <form>#}
{# <div class="form-group has-success">#}
{# <label class="control-label" for="inputSuccess1">请输入用户名</label>#}
{# <input type="text" class="form-control" id="inputSuccess1" aria-describedby="helpBlock2">#}
{##}
{# </div>#}
{# <div class="form-group has-warning">#}
{# <label class="control-label" for="inputWarning1">请输入密码</label>#}
{# <input type="text" class="form-control" id="inputWarning1">#}
{# </div>#}
{# <div class="form-group has-error">#}
{# <label class="control-label" for="inputError1">请确认密码</label>#}
{# <input type="text" class="form-control" id="inputError1">#}
{# </div>#}
{# <div class="has-warning form-group">#}
{# <input type="submit" value="提交" class="btn btn-info pull-right">#}
{##}
{# </div>#}
{##}
{##}
{##}
{# </form>#}
{# </div>#}
{# </div>#}
{# </div>#}
{#</div>#} <div class="container col-md-5 col-md-offset-3">
<div >
<div>
<form action="" method='post' novalidate>
{% csrf_token %} {% for foo in form %}
{# 循环你的 报错的信息#}
<div class="form-group">
<label for="">{{ foo.label }}</label>
{{ foo }}
<span class="pull-right">{{ foo.errors.0 }}</span>
{% if foo.label == 'R pwd' %}
<span class="pull-right">{{ g_error|default_if_none:'' }}</span>
{% endif %} </div> {% endfor %} <div class="nide">
<input type="submit" value = '提交' class="pull-right btn btn-info">
</div> </form>
</div>
</div>
</div> </body>
</html>

注册

主界面: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" id="bs-example-navbar-collapse-9">
<ul class="nav navbar-nav pull-right" >
{# <li class="active"><a href="#">{{ request.COOKIES.name }}</a></li>#}
{# <li><a href="#">{{ request.COOKIES.last_time }}</a></li>#}
<li class="active"><a href="#">{{ request.session.name }}</a></li>
<li><a href="#">{{ request.session.last_time }}</a></li>
<li><a href="{% url 'logout' %}">注销</a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
</div> <div class="bs-example" data-example-id="contextual-table"> <a href="{% url 'add_book' %}" class="btn btn-info pull-right">添加书籍</a>
<table class="table">
<thead> <tr>
<th >序号</th>
<th class="info">名字</th>
<th>价格</th>
<th class="warning">出版时间</th>
<th>出版社</th>
<th class="success">作者</th>
<th>操作</th>
<th>操作</th>
</tr>
</thead> <tbody>
{% for book in book_list %} <tr class="active">
<th scope="row">{{ forloop.counter }}</th>
<td class="info">{{ book.name }}</td>
<td class="danger">{{book.price}}</td>
<td class="success">{{ book.date|date:'Y-m-d' }}</td>
<td class="active"> {{ book.publish.title }}</td>
<td class="warning">
{% for author in book.authors.all %}
{% if not forloop.last %}
{{ author.name }}{{ '|' }}
{% else %}
{{ author.name }}
{% endif %}
{% endfor %} </td>
<td><a href="{% url 'edit_book' book.id %}" class="danger btn btn-info">编辑</a></td> <td><a edid = '{{ book.id }}' class=' btn btn-danger'>删除</a></td>
{# 让这个删除按钮获取要删除的书籍id#} </tr> </tbody> {% endfor %}
</table>
</div>
{% csrf_token %}
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
$('.btn-danger').click(function(){
_this = $(this);
console.log(111,_this)
$.ajax({
url:{% url 'del_book' %},
type:'post',
data:{
deid:_this.attr('edid'),
{#当你点击的时候获取你点击的那个对象的id传送到后台#}
csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val()
},
success: function (data){
var data = JSON.parse(data);
if(data.status){
_this.parent().parent().remove()
}
}
})
})
</script> </body>
</html>

index

编辑: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>
.container{
margin-top: 80px;
}
</style>
</head>
<body> <div class="container">
<div>
<div>
<div class="bs-example" data-example-id="form-validation-states">
<form action="{% url 'edit_book' book_list.id %}}" method="post">
{% csrf_token %}
<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="name" value="{{ book_list.name }}"> </div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning1">价格</label>
<input type="text" class="form-control" id="inputWarning1" name="price" value = {{ book_list.price }}>
</div>
<div class="form-group has-error">
<label class="control-label" for="inputError1">出版时间</label>
<input type="date" class="form-control" id="inputError1" name="date" value="{{ book_list.date|date:'Y-n-d:x' }}">
</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="">#}
{##}
{# </div>#} <div class="form-group has-error">
<label class="control-label" for="inputError1" >出版社</label>
{# <input type="text" class="form-control" id="inputError1" name="publish">#}
<select name="publish" id="inputError1" class="form-control" >
{% for pub in publish_list %}
{% if book_list.publish == pub %}
<option value="{{ pub.id }}" selected>{{ pub.title }}</option>
{% else %}
<option value="{{ pub.id }}" >{{ pub.title }}</option> {% endif %} {% endfor %} </select>
</div> <div class="form-group has-warning">
<label class="control-label" for="inputWarning1">作者</label>
{# <input type="text" class="form-control" id="inputWarning1" name="author">#}
<select name="author" id="" multiple class="form-control">
{% for author in author_list %}
{% if author in book_list.authors.all %}
<option value="{{ author.id }}" selected >{{ author.name }}</option>
{% else %}
<option value="{{ author.id }}" >{{ author.name }}</option> {% endif %} {% endfor %} </select>
</div> <input type="submit" value="提交" class="btn btn-info pull-right">
</form>
</div>
</div>
</div>
</div> </body>
</html>

编辑

添加:add_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>
.container{
margin-top: 80px;
}
</style>
</head>
<body> <div class="container">
<div>
<div>
<div class="bs-example" data-example-id="form-validation-states">
<form action="{% url 'add_book' %}" method="post">
{% csrf_token %}
<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="name" value = {{ book_list.name }} > </div>
<div class="form-group has-warning">
<label class="control-label" for="inputWarning1">价格</label>
<input type="text" class="form-control" id="inputWarning1" 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="date">
</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="">#}
{##}
{# </div>#} <div class="form-group has-error">
<label class="control-label" for="inputError1">出版社</label>
{# <input type="text" class="form-control" id="inputError1" name="publish">#}
<select name="publish" id="inputError1" class="form-control" selected>
{% for publish in publish_list %}
<option value="{{ publish.id }}">{{ publish.title }}</option>
{% endfor %} </select>
</div> <div class="form-group has-warning">
<label class="control-label" for="inputWarning1">作者</label>
{# <input type="text" class="form-control" id="inputWarning1" name="author">#}
<select name="author" id="" multiple class="form-control" multiple>
{% for author in author_list.all %}
<option value="{{ author.id }}" >{{ author.name }}</option>
{% endfor %} </select>
</div> <input type="submit" value="提交" class="btn btn-info pull-right">
</form>
</div>
</div>
</div>
</div> </body>
</html>

add_book

跳转 hint

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.container{
margin-top: 80px;
}
</style>
</head>
<body> <script type="text/javascript">
alert('您输入的用户名不存在 请去注册');
setTimeout() </script> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> <div class="container">
<div>
<div>
<a href="{% url 'register' %}" class="btn btn-lg btn-info btn-block" >Register</a>
</div>
</div>
</div> </body>
</html>

hint

图书管理系统 基于form组件的更多相关文章

  1. 基于Form组件实现的增删改和基于ModelForm实现的增删改

    一.ModelForm的介绍 ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 lab ...

  2. 图书管理系统(无中间件,用装饰器的)-----未基于FORM组件

    目的:实现图书的增删改查 models.py from django.db import models # Create your models here. class Book(models.Mod ...

  3. 2.1博客系统 |基于form组件和Ajax实现注册登录

    基于forms组件和Ajax实现注册功能 1 基于forms组件设计注册页面 --点击头像 === 点击input --头像预览: 修改用户选中的文件对象:获取文件对象的路径:修改img的src属性, ...

  4. Django基于form组件实现注册校验

    一 基本流程 1 创建form组件对应的类,比如LoginForm 2 前端的三种渲染方式: 渲染方式三种: 1 <form action="" novalidate met ...

  5. x学生管理系统(用中间件)-------基于FORM组件

    目的:实现学生,老师,课程的增删改查 models.py from django.db import models # Create your models here. class UserInfo( ...

  6. 基于FORM组件学生管理系统【中间件】

    目的:实现学生,老师,课程的增删改查 models.py from django.db import models # Create your models here. class UserInfo( ...

  7. day 77 基于form组件的注册功能

    Form 表单 py文件 from django import forms #定义一个form类注册用 class RegForm (forms.Form): username =forms.Char ...

  8. 书籍管理系统 -----没有form组件

    urls: from django.contrib import admin from django.urls import path,re_path from first import views ...

  9. django----基于Form组件实现的增删改和基于ModelForm实现的增删改

    一.ModelForm的介绍 ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 lab ...

随机推荐

  1. Unity 依赖注入

    关于Ioc的框架有很多,比如astle Windsor.Unity.Spring.NET.StructureMap,我们这边使用微软提供的Unity做示例,你可以使用Nuget添加Unity,也可以引 ...

  2. 老司机谈APK瘦身套路-图片资源篇

    APK作为Android系统安装包的文件格式,一直备受广大用户的关注,APK越是臃肿肥大,在下载安装过程中,它们耗费的流量会越多,安装等待时间也会越长:对于产品本身,意味着下载转化率会越低(因为竞品中 ...

  3. js中in关键字的用法

    1. 在For...In 声明用于对数组或者对象的属性进行循环/迭代操作. 例子:var a = new Array; for(x in a){ console.log(x); } 2. 判断对象是否 ...

  4. 激活 IntelliJ IDEA

    1.点击下面的链接下载 JetbrainsIdesCrack-4.2-release.jar 链接:https://pan.baidu.com/s/1eNY_bwxF7Efl4QG0yh6l1A  提 ...

  5. SqlServer常用语句

    首先,写这个的原因是我其实sql语句不太行,总觉得自己写得很乱,好像也没有系统学习过,借此复习和与大家探讨 No.1 关于查询时间区间是否重叠的sql语句 问题是这样:插入之前,想查询同User是否其 ...

  6. 启动Hadoop时候datanode没有启动的原因及解决方案

    有时候我们start-dfs.sh启动了hadoop但是发现datanode进程不存在 一.原因 当我们使用hadoop namenode -format格式化namenode时,会在namenode ...

  7. Redis 的 Lua 脚本支持

    Redis 2.6.0 内置的Lua Script支持,可以在Redis的Server端一次运行大量逻辑. 整个Script默认是在一个事务里的. Script里涉及的所有Key尽量用变量,从外面传入 ...

  8. HTTP协议的内容协商

    一.前言 我们在日常的抓包过程中经常可以看到以Accept开头的请求首部,比如:Accept-Language 有一个q值,肯定有人好奇在HTTP规范中为什么要定义这个q值:还有在响应首部有一个名为V ...

  9. JQuery图片加载显示loading和加载失败默认图片

    在很多时候我们需要用到图片加载功能,在网上搜资料后就想为了以后使用方便重新整理了下,结果如图: 最后一张是加载失败显示的图片,没找到合适的图片,先用他顶替. 页面引用 <div class=&q ...

  10. Runtime 打开记事本

    package com.direct.str; import java.io.IOException; public class RunTimeDemo { /** * @param args */ ...