Django学习笔记1
重点在注释#
1.views.py
from django.shortcuts import render
from django.http import *
#from django.template import RequestContext,loader # Create your views here. def index(request):#request是已经封装好的,包括请求头
# temp=loader.get_template('booktest/test.html')#loader表示加载,得到某模板
# return HttpResponse(temp.render())#将模板渲染
return render(request,'booktest/test.html',)#是上面两行代码的封装,简单些,
#'booktest/test.html'是templates下的路径,但我们还需要templates的路径,需要到setting.py下配置
2.setting.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#我猜是获得book_hero的路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],#路径拼接,BASE_DIR在第16行,后面加上templates,
# C:\Users\OOK\Desktop\book_hero\templates 如果templates放在app里,则这里不用配置
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
3.views.py里面调用models
from django.shortcuts import render
from django.http import *
#from django.template import RequestContext,loader #.表示当前目录
#from . import models #用这种方式需要models.BookInfo这样去调用models里面的类
from .models import * #用这种方式简单些,可以直接写models里面的类 # Create your views here. def index(request):#request已经封装好了,包括请求头
# temp=loader.get_template('booktest/test.html')
# return HttpResponse(temp.render()) #bookList=models.BookInfo.objects.all()
bookList=BookInfo.objects.all() content={'list':bookList} return render(request,'booktest/test.html',content)#是上面两行代码的封装,简单些,
#'booktest/test.html'是templates下的路径,但我们还需要templates的路径,需要到setting.py下配置
Django学习笔记1的更多相关文章
- Django 学习笔记之四 QuerySet常用方法
QuerySet是一个可遍历结构,它本质上是一个给定的模型的对象列表,是有序的. 1.建立模型: 2.数据文件(test.txt) 3.文件数据入库(默认的sqlite3) 入库之前执行 数据库同步命 ...
- Django 学习笔记之三 数据库输入数据
假设建立了django_blog项目,建立blog的app ,在models.py里面增加了Blog类,同步数据库,并且建立了对应的表.具体的参照Django 学习笔记之二的相关命令. 那么这篇主要介 ...
- Django学习笔记(五)—— 表单
疯狂的暑假学习之 Django学习笔记(五)-- 表单 參考:<The Django Book> 第7章 1. HttpRequest对象的信息 request.path ...
- Django学习笔记(三)—— 型号 model
疯狂暑期学习 Django学习笔记(三)-- 型号 model 參考:<The Django Book> 第5章 1.setting.py 配置 DATABASES = { 'defaul ...
- Django 学习笔记(二)
Django 第一个 Hello World 项目 经过上一篇的安装,我们已经拥有了Django 框架 1.选择项目默认存放的地址 默认地址是C:\Users\Lee,也就是进入cmd控制台的地址,创 ...
- Django 学习笔记(五)模板标签
关于Django模板标签官方网址https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.IF标签 Hello World/vi ...
- Django 学习笔记(四)模板变量
关于Django模板变量官方网址:https://docs.djangoproject.com/en/1.11/ref/templates/builtins/ 1.传入普通变量 在hello/Hell ...
- Django 学习笔记(三)模板导入
本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...
- Django 学习笔记(七)数据库基本操作(增查改删)
一.前期准备工作,创建数据库以及数据表,详情点击<Django 学习笔记(六)MySQL配置> 1.创建一个项目 2.创建一个应用 3.更改settings.py 4.更改models.p ...
- Django 学习笔记(六)MySQL配置
环境:Ubuntu16.4 工具:Python3.5 一.安装MySQL数据库 终端命令: sudo apt-get install mysql-server sudo apt-get install ...
随机推荐
- WPF中使用定时器 DispatcherTimer 做TCP连接中的心跳 HeartBeat
开发过程中经常遇到定时触发的需求,如:TCP/IP连接中,使用心跳包保持连接或检测连接是否已经中断. WPF中有多种定时器: 1.using System.Windows.Threading; 代码如 ...
- 【Python】安装配置Anaconda
优点:解决Python 库依赖问题 清华安装镜像 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
- unity3d代码优化标准
转载自:https://blog.csdn.net/m0_37283423/article/details/84378384 代码优化 ● 尽可能使用for来代替foreach:每次foreach会产 ...
- xlwings: Write Excel macro using python instead of VBA
i want to write Excel macros to deal with the data, but i am not familiar with VBA language. so i de ...
- Android已上线应用开源分享中(第一季)
这是我上线的第一个android应用,在百度.腾讯.豌豆荚等平台测试通过,也有了部分用户,还是可以的啊,哈哈.现在分享给大家,当然,源码我也会分享. 1.软件是一个管理wifi的小工具 (1)查询.连 ...
- pt-heartbeat(percona toolkit)
pt-heartbeat是用来监控主从延迟的一款percona工具,现在我们大部分的MySQL架构还是基于主从复制,例如MHA,MMM,keepalived等解决方案.而主从环境的话,我们很关心的就是 ...
- mysql安装错误之->ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
有时候,当我们使用“mysql”.“mysqladmin”.“mysqldump”等命令管理数据库时,服务器抛出类似如下错误: 一.错误现场还原:下面我们通过三种方式来连接,然后观察提示的错误信息: ...
- NSJSONSerialization能够处理的JSONData
NSJSONSerialization能够处理的JSONData You use the NSJSONSerialization class to convert JSON to Foundation ...
- php大文件上传失败的原因及解决方法
为什么上传大文件总是失败,上传小文件就没有问题.关于PHP大文件上传失败的原因及解决方法如下: 第1种情况:文件上传时存放文件的临时目录必须是开启的并且是 PHP 进程所有者用户可写的目录.如果未指定 ...
- 【深入理解JAVA虚拟机】第5部分.高效并发.1.Java内存模型与线程。
1.概述 摩尔定律:描述处理器晶体管数量与运行效率之间的发展关系.Amdahl定律:通过系统中并行化与串行化的比重来描述多处理器系统能获得的运算加速能力. 从摩尔定律到Amdahl定律的转变,代表了近 ...