Django学习笔记3
From the last two parts, we know, by using the HttpResponse we can return text to the web page, and by using admin site we can edit our question. Then we have to show our polls on the page. We are going to show latest 5 questions link. We can click it to see the details.
1.Showing the question link
Work with views.py index function. We only need an argument, request from the server. Then call the Question model and visit the latest five question by coding Question.objects.order_by('-pub_time')[:5]. Of course we can code the display lines in it. But it will be troublesome when we have a lot to write and edit. So why not write it in the html file to make the structure more clearly. So now let's create a templates directory in the app polls and subdirectory polls to contain the html files.Why not directly put the html files in the templates? I'm not satisfied with the answer. But from my understanding, the program will traverse the INSTALLED_APPS and look into the templates. It will go into the first one that matches the html name. So we need one subdirectory with the app name to differentiate the html files with the same name in different apps. Create templates->polls->index.html, detail.html.

But how do we correspond the templates with views.py function. In the views.py index function, we load the designated templates html file, pass a context which is a dictionary form to deliver arguments, render it, namely to process it and make it in a state and return the HttpResponse rendered result. But we can just return the render() shortcut to tackle all of them. Amazing render!!!

next version:

next version:

Here comes the html edition. But it has a shortcome that the url is hardcoded. Recall the url path we wrote before, path('<int:question_id>/', views.detail, name='detail'), the name is nickname of the path, so that we can use the nickname which will be easier for us to change another path in the future.

next version:

next version: to differentiate the same url name in different apps, we can write app_name = 'polls' in polls/urls.py top of the INSTALLED_APPS.

2. showing the choice.
We've writtern the url link to the detail of the question. So go to the detail funtion in views.py and deliver the question to the html file, meanwhile an exception is necessary to takcle the problem of question=none. And we code the question and choices display lines in detail.html.

next version:



Django学习笔记3的更多相关文章
- 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 ...
随机推荐
- 在香蕉派的树莓派系统上配置 Syncthing 自启动
在香蕉派的树莓派系统上配置 Syncthing 自启动 —— 魏刘宏 2020 年 1 月 19 日 首先做个名词解释,” 香蕉派” 是国内一款山寨树莓派的硬件产品,” 树莓派系统” 指的是”rasp ...
- ansible基本使用(一)
ansible是什么? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量 ...
- 线段树区间染色 ZOJ 1610
Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...
- P1967 货车运输【LCA】【生成树】
题目描述 A 国有 nn 座城市,编号从 11 到 nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重. 现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的 ...
- NIO学习笔记,从Linux IO演化模型到Netty—— Netty零拷贝
Netty的中零拷贝与上述零拷贝是不一样的,它并不是系统层面上的零拷贝,只是相对于ByteBuf而言的,更多的是偏向于数据操作优化这样的概念. Netty中的零拷贝: 1.CompositeByteB ...
- jdk8配置
Java SE Development Kit 8u241 下载 https://www.oracle.com/java/technologies/javase/javase-jdk8-downloa ...
- C#调用Crypto++库AES ECB CBC加解密
本文章使用上一篇<C#调用C++类库例子>的项目代码作为Demo.本文中,C#将调用C++的Crypto++库,实现AES的ECB和CBC加解密. 一.下载Crypto 1.进入Crypt ...
- C#设计模式学习笔记:设计原则
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8287784.html,记录一下学习过程以备后续查用. 写代码也是有原则的,我们之所以使用设计模式,主要是为了 ...
- (int)、int.Parse()、int.TryParse()、Convert.ToInt32()区别
请看代码: //1.null. //int i1 = (int)null;//编译时报错:无法将“null”转换为“int”,因为后者是不可以为“null”的值类型. //int i2 = int.P ...
- Hadoop学习之路(7)MapReduce自定义排序
本文测试文本: tom 20 8000 nancy 22 8000 ketty 22 9000 stone 19 10000 green 19 11000 white 39 29000 socrate ...