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 ...
随机推荐
- 项目经验:GIS<MapWinGIS>建模第一天
随便记录下项目的进展情况: GIS平台系统,实现整个供水系统的协调与统一.系统以管网为基础依据,建立可实现供水管网规划设计.输配管理.图档管理.抢修辅助决策及综合查询.统计等功能. 本文档的预期读者是 ...
- Python爬虫教程-16-破解js加密实例(有道在线翻译)
python爬虫教程-16-破解js加密实例(有道在线翻译) 在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签 ...
- Linux(centos7)安装maven3.5
1.创建一个maven文件夹 [root@MiWiFi-R3-srv ~]# mkdir /usr/local/maven 1 2.上传apache-maven-3.5.0-bin.tar.gz到/u ...
- myEclipse mybatis自动生成工具xml配置
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE generatorConfiguration ...
- 云计算之概念——IaaS、SaaS、PaaS、Daas
云计算通俗来说就是输入/输出和计算不在一个主机上.计算要用到计算设备,计算设备一般是指CPU.内存和硬盘,输入/输出设备一般是指键盘.鼠标.显示器.耳机.音响.话筒等外设.而我们的个人计算机是使用主板 ...
- linux和aix内核参数检查
安装oracle软件时需要设置操作系统级别的用户限制,后期检查可以使用如下命令,方便问题的排查工作 linux: tail -15 /etc/security/limits.conf tail -30 ...
- CentOS 6 网络yum源配置
# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update ...
- Windows 10 KMS 手工激活
第一.安装好Win10系统,不需要安装其他激活工具.第二.是删除默认序列号,打开命令提示符(管理员),运行 slmgr.vbs -upk,可提示已卸载了序列号. slmgr /ipk W269N-WF ...
- CopyOnWriteArrayList对比ArrayList
ArrayList非线程安全,CopyOnWriteArrayList线程安全 ArrayList添加元素的时候内部会预先分配存储空间,CopyOnWriteArrayList每次添加元素都会重新co ...
- 移动端开发 rem 案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...