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. luoguP1003 铺地毯 题解(NOIP2011)

    luoguP1003 铺地毯  题目 #include<cstdio> #include<cstdlib> #include<cstring> #include&l ...

  2. redis 持久化 哨兵 主从

    Redis搭建步骤 环境: 三台机器  centos7 关闭防火墙 selinux Redis版本 3.0.5 依赖环境 yum install gcc-c++ ruby rubygems –y 把版 ...

  3. CodeChef A String Game(SG)

    A String Game   Problem code: ASTRGAME   Submit All Submissions   All submissions for this problem a ...

  4. F Find the AFei Numbers

    链接:https://ac.nowcoder.com/acm/contest/338/F来源:牛客网 题目描述 AFei loves numbers. He defines the natural n ...

  5. C# Base64编码解码 ,Md5、Rsa加密解密

    using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Clas ...

  6. 二进制部署k8s

    一.二进制部署 k8s集群 1)参考文章 博客: https://blog.qikqiak.com 文章: https://www.qikqiak.com/post/manual-install-hi ...

  7. MapReduce的序列化机制

    MapReduce自己实现了一套序列化机制,通过实现Writable接口, 重写DateInput和DateOutPut方法,实现数据的序列化和反序列化, 相比于JDK自带的序列化,MapReduce ...

  8. 【记录】spring/springboot 配置mybatis打印sql

    ======================springboot mybatis 打印sql========================================== 方式 一: ##### ...

  9. Cent OS 7 VNC 安装

    关闭防火墙 关闭selinux 挂载光盘到本地 #yum install tigervnc-server -y #cp /lib/systemd/system/vncserver@.service / ...

  10. (ACM模板)集合set

    #include<iostream> #include<cstdio> #include<set> using namespace std; int main() ...