Django学习(五) 定义视图以及页面模板
请求解析一般都是通过请求的request获取一定参数,然后根据参数做一定业务逻辑判断,这其中可能包括查询数据库,然后将需要返回的数据封装成一个HttpResponse返回。
代码如下:
这是一个简单的处理请求的函数,对应之前url映射的 url(r'^articles/([0-9]{4})/$', views.year_archive),django会将url中用()包起来的内容作为变量传给函数,此处year_archive中的year变量就是([0-9]{4})代表的值。
Article.objects.filter(pub_date__year=year)过滤出发布日期是year的数据。注意pub_date__year,数据库表中只有pub_date字段,pub_date__year代表值取年。
所以可以认为这是django默认提供的一种接口查询方式。
然后将返回的list列表和year再组装成一个字典数据。
最后调用django提供的函数render返回。render完成的事情其实就是选择一个模板,创建一个Context对象,然后将这些数据放到创建的一个HttpResponse中去。
mysite/news/views.py from django.shortcuts import render from .models import Article def year_archive(request, year):
a_list = Article.objects.filter(pub_date__year=year)
context = {'year': year, 'article_list': a_list}
return render(request, 'news/year_archive.html', context)
上面的代码其实是对以下数据进行了封装,render中封装了以下代码的作用。
from django.template import Template, Context
from django.http import HttpResponse
import datetime def current_datetime(request):
now = datetime.datetime.now()
# Simple way of using templates from the filesystem.
# This is BAD because it doesn't account for missing files!
fp = open('/home/djangouser/templates/mytemplate.html')
t = Template(fp.read())
fp.close()
html = t.render(Context({'current_date': now}))
return HttpResponse(html)
Django学习(五) 定义视图以及页面模板的更多相关文章
- Django学习笔记之视图高级-HTTP请求与响应
Django限制请求method 常用的请求method GET请求 GET请求一般用来向服务器索取数据,但不会向服务器提交数据,不会对服务器的状态进行更改.比如向服务器获取某篇文章的详情. POST ...
- Django学习 之后端视图与ajax
一.Ajax简介 在此之前你一定需要先学习下JavaScript JSON 可见: 前端学习 之 JavaScript 之 JSON 1.简单介绍 我们以前知道的前端向后端发送数据的方式有: GET: ...
- Django学习笔记之视图高级-CSV文件生成
生成CSV文件 有时候我们做的网站,需要将一些数据,生成有一个CSV文件给浏览器,并且是作为附件的形式下载下来.以下将讲解如何生成CSV文件. 生成小的CSV文件 这里将用一个生成小的CSV文件为例. ...
- Django学习笔记之视图高级-类视图
类视图 在写视图的时候,Django除了使用函数作为视图,也可以使用类作为视图.使用类视图可以使用类的一些特性,比如继承等. View django.views.generic.base.View是主 ...
- Django学习---路由url,视图,模板,orm操作
Django请求周期 url -> 路由系统 ->函数或者类 -> 返回字符串 或者 模板语言 Form表单提交: 点击提交 -> 进入url系统 -> 执行函数 ...
- Django学习日记06_视图_URLconf、View
URLconf Django通过URLconf来响应网页请求,在项目设置settings.py中,设定了ROOT_URLCONF值来指定默认的URLconf(即mysite.urls),当HTTPRe ...
- Django学习之 - 基础视图函数
视图:Views 获取用户请求的方法: 1: request.GET 2: request.POST 3: request.FILES # checkbox 等多选文件 4:request.POST. ...
- Django学习路35_视图使用方法(复制的代码) + 简单总结
from django.shortcuts import render,redirect from django.http import HttpResponse,JsonResponse from ...
- django 学习-6 定义模型--数据库的使用
1.service mysqld start 首先数据库是可用的 2.rpm -qa |grep MySQL-python 这个包是存在的 3.vim settings 修改databases 加 ...
随机推荐
- Loadrunner根据PV量来确定需要进行压测的并发量
在实际做压力测试的过程中,我们有时不知道用怎样的并发量比较好,下面是几个用PV量去确定并发量的公式,这个在我们公司是比较适用的,大家可以根据自己的业务进行运算. 方法一:这个方法是我在网上查到的80- ...
- App Store审核指南中文版(2014.10.11更新)
App Store审核指南中文版(2014.10.11更新) 2014-10-11 16:36 编辑: suiling 分类:AppStore研究 来源:CocoaChina 2 8657 App ...
- hdu 3191 How Many Paths Are There
http://acm.hdu.edu.cn/showproblem.php?pid=3191 这道题求次短路经和路径数 #include <cstdio> #include <cst ...
- Js,alert出现乱码问题,赶紧记下来,额~~~
原文 http://www.cnblogs.com/still-windows7/archive/2011/08/28/2156010.html 前些天还可以正常使用的js文件,在添加了一些东西后,其 ...
- ORA-3136报错
当使用错误的用户名或密码登陆数据库时,会提示如下报错内容: bash-4.1$ sqlplus a/a@test SQL*Plus: Release 10.2.0.4.0 - Production o ...
- 单路CPU性能排名 更新于2015.10.6
http://itianti.sinaapp.com/index.php/cpu 排名 处理器 图例 分数 1 Intel Xeon E5-2699 v3 @ 2.30GHz 22892 2 Inte ...
- Gray Code 解答
Question The gray code is a binary numeral system where two successive values differ in only one bit ...
- Hdu1401-Solitaire(双向bfs)
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...
- python练习linux下创建路径
#coding=utf-8 import os class MakeDirectory(): def mkdir(self,path): # 去除首位空格 path=path.strip() # 去除 ...
- (3)选择元素——(5)为项目列表加样式(Styling list-item levels)
Let's suppose that we want the top-level items, and only the top-level items, to be arranged horizon ...