Django~Views
In Django, web pages and other content are delivered by views.
To get from a URL to a view, Django uses what are known as ‘URLconfs’. A URLconf maps URL patterns (described as regular expressions) to views.
write views
add urls


404
Each view is responsible for doing one of two things: returning an HttpResponse object containing the content for the requested page, or raising an exception such as Http404.
PDF,.XML,ZIP ^^^^^^^
Your view can read records from a database, or not. It can use a template system such as Django’s – or a third-party Python template system – or not. It can generate a PDF file, output XML, create a ZIP file on the fly, anything you want, using whatever Python libraries you want.
Create Templates
First, create a directory called templates in your polls directory. Django will look for templates in there
名字空间
路径:polls/templates/polls/index.html. .否则

没有加模板前 添加模板后

templates中内容
<ul> for 循环</ul>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{question.id}}/">{{question.question_text}}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
省事的render

return render(request, 'polls/index.html', context)
The render() function takes the request object as its first argument, a template name as its second argument and a dictionary as its optional third argument. It returns an HttpResponse object of the given template rendered with the given context.
404问题
try except render


加上名字空间
ulsr.py中app_name=’polls’
index 中polls:detail
<li><a href="{% url 'polls:detail' question.id %}">{{question.question_text}}</a></li>
Django~Views的更多相关文章
- Django views 中 View decorators
decorators(装饰器) 1. require_http_methods 在django.views.decorators.http中,可以用来限制请求的权限. require_http_met ...
- django views视图函数返回值 return redirect httpresponse总结
django views视图函数返回值 return redirect render httpresponse总结
- Django Views and URLconfs
碧玉妆成一树高,万条垂下绿丝绦. 不知细叶谁裁出,二月春风似剪刀. 原文尽在:http://djangobook.com/ 转载请注明出处:http://www.cnblogs.com/A-FM/p/ ...
- django views视图函数
Django views.py视图文件 一. 创建views.py文件,在工程文件夹根目录创建views.py视图文件,其实任意文件名都可以,使用views是为了遵循传统. 注:所有的views函数都 ...
- Django Views Decorator
Django的试图函数的装饰器主要有: HTTP请求方法 条件视图处理 GZip压缩 改变页眉 缓存 官网文档 HTTP请求方法 该装饰器是设置允许访问HTTP协议的方法,装饰器在django.vie ...
- Django—views系统:views基础
Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...
- Django -- Views and URLconf
1 创建工程 django-admin startproject mysite && cd mysite 2 创建应用 python manage.py startapp blog 3 ...
- Django Views(视图函数)
http请求中产生两个核心对象: http请求:HttpRequest对象 http响应:HttpResponse对象 所在位置:django.http 之前我们用到的参数request就是HttpR ...
- django views视图
视图函数简称视图,本质上是一个简单的python函数,它接受web请求并且返回web响应:响应的内容可以是HTML网页.重定向.404错误.XML文档或图像等任何东西,但是,无论视图本身是个什么处理逻 ...
随机推荐
- jq实现点击弹出框代码
废话不多说,先贴代码吧 <script> function showBg() { //定义 showBg 函数 var bh = $("body").height(); ...
- app的meta
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> < ...
- jquery动态改变my97日期格式
$('#qsrq').unbind('focus'); $('#zzrq').unbind('focus'); $('#qsrq').bind('focus', function () { Wdate ...
- nyoj 252 01串 动态规划( java)
当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...
- git学习教程
猴子都能懂的GIT入门 http://backlogtool.com/git-guide/cn/ 廖雪峰的Git教程 http://www.liaoxuefeng.com/wiki/001373951 ...
- Android手机如何通过USB共享网络给Mac?
最近网络挂了,mac不能上网查资料,心情非常毛躁,急切寻求用mac蹭WiFi的方法. 没有找到电脑端破解WiFi密码的软件,手头的Android手机没有root,也无法查看WiFi密码--->破 ...
- 读w3c中文教程对键盘事件解释的感想 -遁地龙卷风
写这篇博文源于w3c中文教程对键盘事件的解释, onkeydown 某个键盘按键被按下 onkeypress 某个键盘按键被按下并松开 onkeyup 某个键盘按键被松开 可在实践中发现 只注册key ...
- sql种类
- python 内置函数 : compile()
这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. 参数source是一串字符串的源码,或者是 ...
- iOS 开发 初级:应用内购买 In-App Purchase
http://blog.csdn.net/songrotek/article/details/8680415 现在有很多应用都使用了In-App Purchase,虽然对于很多用户来说,可能并不喜欢甚 ...