视图:
视图(view)组成了Django应用程序里很多(有时候几乎是全部)的逻辑。
它们的定义实际上却很简单: 它们是连接到一个或多个定义URL上的Python函数,
这些函数都返回一个HTTP响应对象。
node2:/app/mysite/blog#cat view.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# from django.shortcuts import render, render_to_response
from .models import *
# Create your views here.
from django.http import HttpResponse
from django.template import loader
def index(req):
# get all blogpost objects
blog_list = BlogPost.objects.all()
print blog_list
template = loader.get_template('index.html')
context = {
'blog_list':blog_list
}
return HttpResponse(template.render(context, req))
------------------------------------------
Content-Type: text/html; charset=utf-8
<html>
<style type="text/css">
body{color:#efd;background:#453;padding:0 5em;margin:0}
h1{padding:2em 1em;background:#675}
h2{color:#bf8;border-top:1px dotted #fff;margin-top:2em}
p{margin:1em 0}
</style>
<body>
<h1>虫师blog</h1>
<h3>大人不华,君子务实</h3>
<h2>www</h2>
<p>1,September 29th</p>
<p>呃呃呃</p>
<h2>赵杨健</h2>
<p>1,September 28th</p>
<p>达到达到</p>
<h2>9999</h2>
<p>1,September 27th</p>
<p>999999999</p>
<h2>ttt</h2>
<p>1,September 26th</p>
<p>tttttttttt</p>
<h2>aabbcc</h2>
<p>1,September 26th</p>
<p>112233aabbcc</p>
<h2>测试</h2>
<p>1,September 26th</p>
<p>1111</p>
</body>
</html>
------------------------------------------
返回HTTP响应对象给模板(前台)