from django.shortcuts import HttpResponse, render, redirect
from app01 import models
# Create your views here. # 展示出版社列表
def publisher_list(request):
# 去数据库查出所有的出版社,填充到HTML中,给用户返回
ret = models.Publisher.objects.all().order_by("id")
return render(request, "publisher_list.html", {"publisher_list": ret}) # 添加新的出版社
def add_publisher(request):
error_msg = ""
# 如果是POST请求,我就取到用户填写的数据
if request.method == "POST":
new_name = request.POST.get("publisher_name", None)
if new_name:
# 通过ORM去数据库里新建一条记录
models.Publisher.objects.create(name=new_name)
# 引导用户访问出版社列表页,查看是否添加成功 --> 跳转
return redirect("/publisher_list/")
else:
error_msg = "出版社名字不能为空!"
# 用户第一次来,我给他返回一个用来填写的HTML页面
return render(request, "add_publisher.html", {"error": error_msg}) # 删除出版社的函数
def delete_publisher(request):
print(request.GET)
print("=" * 120)
# 删除指定的数据
# 1. 从GET请求的参数里面拿到将要删除的数据的ID值
del_id = request.GET.get("id", None) # 字典取值,娶不到默认为None
# 如果能取到id值
if del_id:
# 去数据库删除当前id值的数据
# 根据id值查找到数据
del_obj = models.Publisher.objects.get(id=del_id)
# 删除
del_obj.delete()
# 返回删除后的页面,跳转到出版社的列表页,查看删除是否成功
return redirect("/publisher_list/")
else:
return HttpResponse("要删除的数据不存在!") # 编辑出版社
def edit_publisher(request):
# 用户修改完出版社的名字,点击提交按钮,给我发来新的出版社名字
if request.method == "POST":
print(request.POST)
# 取新出版社名字
edit_id = request.POST.get("id")
new_name = request.POST.get("publisher_name")
# 更新出版社
# 根据id取到编辑的是哪个出版社
edit_publisher = models.Publisher.objects.get(id=edit_id)
edit_publisher.name = new_name
edit_publisher.save() # 把修改提交到数据库
# 跳转出版社列表页,查看是否修改成功
return redirect("/publisher_list/")
# 从GET请求的URL中取到id参数
edit_id = request.GET.get("id")
if edit_id:
# 获取到当前编辑的出版社对象
publisher_obj = models.Publisher.objects.get(id=edit_id)
return render(request, "edit_publisher.html", {"publisher": publisher_obj})
else:
return HttpResponse("编辑的出版社不存在!") # 展示书的列表
def book_list(request):
# 去数据库中查询所有的书籍
all_book = models.Book.objects.all()
# 在HTML页面完成字符串替换(渲染数据)
return render(request, "book_list.html", {"all_book": all_book}) # 删除书籍
def delete_book(request):
# 从URL里面获取要删除的书籍的id值
delete_id = request.GET.get("id") # 从URL里面取数据
# 去删除数据库中删除指定id的数据
models.Book.objects.get(id=delete_id).delete()
# 返回书籍列表页面, 查看是否删除成功
return redirect("/book_list/") # 添加书籍
def add_book(request):
if request.method == "POST":
print(request.POST)
print("=" * 120)
# {"book_title": "跟金老板学开车", "publisher": 9}
new_title = request.POST.get("book_title")
new_publisher_id = request.POST.get("publisher")
# 创建新书对象,自动提交
models.Book.objects.create(title=new_title, publisher_id=new_publisher_id) # 用出版社对象创建
# publisher_obj = models.Publisher.objects.get(id=new_publisher_id)
# models.Book.objects.create(title=new_title, publisher=publisher_obj) # 返回到书籍列表页
return redirect("/book_list/") # 取到所有的出版社
ret = models.Publisher.objects.all()
return render(request, "add_book.html", {"publisher_list": ret}) # 编辑书籍
def edit_book(request):
if request.method == "POST":
# 从提交的数据里面取,书名和书关联的出版社
edit_id = request.POST.get("id")
new_title = request.POST.get("book_title")
new_publisher_id = request.POST.get("publisher")
# 更新
edit_book_obj = models.Book.objects.get(id=edit_id)
edit_book_obj.title = new_title # 更新书名
edit_book_obj.publisher_id = new_publisher_id # 更新书籍关联的出版社
# 将修改提交到数据库
edit_book_obj.save()
# 返回书籍列表页面,查看是否编辑成功
return redirect("/book_list/") # 返回一个页面,让用户编辑书籍信息
# 取到编辑的书的id值
edit_id = request.GET.get("id")
# 根据id去数据库中把具体的书籍对象拿到
edit_book_obj = models.Book.objects.get(id=edit_id)
print(edit_book_obj.id)
print(edit_book_obj.title)
print(edit_book_obj.publisher) # 取到当前书籍对象关联的出版社对象
print(edit_book_obj.publisher_id) # 取到当前书籍对象关联的出版社的id值 ret = models.Publisher.objects.all()
return render(
request,
"edit_book.html",
{"publisher_list": ret, "book_obj": edit_book_obj}
) # 作者列表
def author_list(request):
# author_obj = models.Author.objects.get(id=1)
# print(author_obj.book.all())
# print("=" * 120) # 查询所有的作者
all_author = models.Author.objects.all()
return render(request, "author_list.html", {"author_list": all_author}) # 添加作者
def add_author(request):
if request.method == "POST":
print("in post...")
# 取到提交的数据
new_author_name = request.POST.get("author_name")
# post提交的数据是多个值的时候一定会要用getlist,如多选的checkbox和多选的select
books = request.POST.getlist("books")
# 创建作者
new_author_obj = models.Author.objects.create(name=new_author_name)
# 把新作者和书籍建立对应关系,自动提交
new_author_obj.book.set(books)
# 跳转到作者列表页面,查看是否添加成功!
return redirect("/author_list/") # 查询所有的书籍
ret = models.Book.objects.all()
return render(request, "add_author.html", {"book_list": ret}) # 删除作者
def delete_author(request):
# 从URL里面取到要删除的作者id
delete_id = request.GET.get("id")
#根据ID值取到要删除的作者对象,直接删除
# 1. 去作者表把作者删了
# 2. 去作者和书的关联表,把对应的关联记录删除了
models.Author.objects.get(id=delete_id).delete()
# 返回作者列表页面
return redirect("/author_list/") # 编辑作者
def edit_author(request): # 如果编辑完提交数据过来
if request.method == "POST":
# 拿到提交过来的编辑后的数据
edit_author_id = request.POST.get("author_id")
new_author_name = request.POST.get("author_name")
# 拿到编辑后作者关联的书籍信息
new_books = request.POST.getlist("books")
# 根据ID找到当前编辑的作者对象
edit_author_obj = models.Author.objects.get(id=edit_author_id)
# 更新作者的名字
edit_author_obj.name = new_author_name
# 更新作者关联的书的对应关系
edit_author_obj.book.set(new_books)
# 将修改提交到数据库
edit_author_obj.save()
# 返回作者列表页,查看是否编辑成功
return redirect("/author_list/") # 从URL里面取要编辑的作者的id信息
edit_id = request.GET.get("id")
# 找到要编辑的作者对象
edit_author_obj = models.Author.objects.get(id=edit_id) # 查询所有的书籍对象
ret = models.Book.objects.all()
return render(request, "edit_author.html", {"book_list": ret, "author": edit_author_obj}) def test(request):
print(request.GET)
print(request.GET.get("id"))
return HttpResponse("OK") # 书复习代码
def book_test(request):
# 查询所有的书籍
# book_list = models.Book.objects.all()
# # print(book_list)
# for i in book_list:
# print(i)
# print(i.title)
# print(i.publisher)
# print(i.publisher.name)
# print(i.publisher.addr)
# print("=" * 20) # 增加新书
# new_book_obj = models.Book.objects.create(
# title="新书的名字",
# publisher_id=10
# )
# 增加新书
publisher_obj = models.Publisher.objects.get(id=10)
new_book_obj = models.Book.objects.create(
title="新书的名字2",
publisher=publisher_obj
)
print(new_book_obj) return HttpResponse("o98k")

  

day64 views文件的更多相关文章

  1. 实战:ASP.NET MVC中把Views下面的视图放到Views文件夹外

    园子里写的文章的都是把控制器从传统的项目中的Controllers拿出来单独放,但很少几乎没有把视图从Views拿出去这样的文章,今天来写一个. 其实很简单!一步步解决问题就行了,下面记录如下,供需要 ...

  2. MVC中的Views下面的视图放到Views文件夹外

    实战:把ASP.NET MVC中的Views下面的视图放到Views文件夹外   园子里写的文章的都是把控制器从传统的项目中的Controllers拿出来单独放,但很少几乎没有把视图从Views拿出去 ...

  3. django之创建第7-4个项目-配置views文件实现url传值

    即:怎么实现url?name=xiaodeng&age=28等类似传值处理 1.配置views文件 # Create your views here. #coding:utf-8 from d ...

  4. ASP.NET MVC 5 访问在views文件夹中的JS文件。 ASP.NET MVC html与JS分离

    修改VIEWS文件夹下的web.config文件, 加入下面红色字标识的内容:    <system.webServer>     <handlers>       <r ...

  5. ASP .NET Views文件夹下面的文件找不到

    习惯将页面和它对应的js,css文件放在一个文件夹下,将这些都放在Views文件夹下     运行的时候发现找不到js和css文件 因为在MVC中,是不建议直接去访问Views文件夹的我们建立的ASP ...

  6. asp .net core 读取读取Views文件夹下的js和css

    //读取Views文件夹下的js和css app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFilePro ...

  7. MVC Views文件夹下js无法访问问题解决方案

    出现这个问题是因为webconfig做的限制,可修改相应Views下的webconfig文件来解决. <system.webServer> <handlers> <rem ...

  8. aspnet core 2.0 发布之后没有 views文件夹

    在项目文件里面 增加这个节点: MvcRazorCompileOnPublish 设置为false 是会发布views <PropertyGroup> <PackageTargetF ...

  9. 学习django笔记一:在urls.py中导入sign应用views文件的问题

    >python-admin startproject guest     #创建guest项目 >python3 manage.py startapp sign  #在guest项目中创建 ...

随机推荐

  1. Python 学习笔记20 自定义robot Framework 关键字

    Robot Framework 自定义关键字 Robot framework 自定义了一些关键字我们可以把他们当作函数在设计测试用例的时候使用. 同时RF也提供了许多第三方的库,我们可以自己下载使用. ...

  2. java_第一年_JavaWeb(9)

    JavaBean是一个遵循某种特定写法的Java类,有以下特点: 必需具有一个无参的构造函数 属性必需私有化 私有化的属性必需通过public类型的方法暴露给其它程序,其方法命名也有一定的规范 范例: ...

  3. var、let、const的区别

    var.let.const的区别 var定义的变量,没有块的概念,可以跨块访问, 不能跨函数访问. let定义的变量,只能在块作用域里访问,不能跨块访问,也不能跨函数访问. const用来定义常量,使 ...

  4. 天堂Lineage(單機版)從零開始架設教學 Installing Lineage 3.52 Server - On Windows

      1. [下載原始碼] Using RapidSVN 用checkout      http://l1j-tw-99nets.googlecode.com/svn/trunk/L1J-TW_3.50 ...

  5. python学习第二十六天非固定参数几种情况

    python函数参数传递,位置参数,默认参数,关键词参数,最后介绍一个非固定参数,就可以向函数传递一个列表,元组,字典,具体看看用法 1,有一个* 号的参数情况 def goos_stu(id,*us ...

  6. HDU 6315 Naive Operations 【势能线段树】

    <题目链接> 题目大意: 给出两个序列,a序列全部初始化为0,b序列为输入值.然后有两种操作,add x y就是把a数组[x,y]区间内全部+1,query x y是查询[x,y]区间内∑ ...

  7. 攻防世界--re2-cpp-is-awesome

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/c5802869b8a24033b4a80783a67c858b 1.准备 获取信息 6 ...

  8. RabbitMQ ——简单队列

    一 .概述 我们不从开始就讲述基本的概念,尤其是在Rabbitmq之中有些概念确实比较难以理解,我们首先做的就是将光放提供的消息模型 进行实现,然后再总结一下Rabbitmq之中的基本概念. 二 .基 ...

  9. elasticsearch 基础 —— 索引、更新文档

    索引文档 通过使用 index API ,文档可以被 索引 -- 存储和使文档可被搜索 . 但是首先,我们要确定文档的位置.正如我们刚刚讨论的,一个文档的 _index . _type 和 _id 唯 ...

  10. photoshop中调整图层的颜色深浅明暗

    图像-调整-可选颜色, 选中某一个颜色如绿色,可以将绿色调的深一点或浅一点