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文档或图像等任何东西,但是,无论视图本身是个什么处理逻 ...
随机推荐
- Linux关于添加硬盘的那些事儿:笔记
添加新硬盘:http://note.youdao.com/share/?id=8cf27602cdce36e1d4160f00e9004b00&type=note 关于添加硬盘的那些事儿: ...
- mfc 调试 弹消息
AfxMessageBox(_T("edit change msg")); OutputDebugString(L"jian "); CString str; ...
- 微信公众平台回复链接可以直接访问,但不能是锚文字链接<a>标签
最近在学习微信公众平台开发,由于编辑模式和开发模式不可同时开启,在开发模式下如果访客发送关键字过来暂时无法实现关键词自动回复,客服人员先用链接网址直接回复订阅用户,但请注意不能是文字链接,即<a ...
- Magic Number(Levenshtein distance算法)
Magic Number Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- java系列-使用maven创建web项目(二)
推荐2个maven找jar包配置的网站,只需要搜索关键字即可找到需要的Jar包,非常方便,比如:MySQL就可以找到mysql-connect-Java.jar. http://search.mave ...
- 【C语言入门教程】7.1 结构体类型变量的定义和引用
前面学习了变量和数组这些简单的数据结构,它们的特点是必须使用规定的数据类型.例如数组被定义为整型后,它的所有存储单元都是由整型构成.现实生活中某一类事物的共同属性可能是由不同的数据类型组成的集合,或者 ...
- WPF:自定义路由事件的实现
路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...
- 学习ios(必看经典)牛人40天精通iOS开发的学习方法
学习ios(必看经典)牛人40天精通iOS开发的学习方法 描述 这是一套从一个对iOS开发感兴趣的学员到iOS开发高手的系统.专业的课程体系.以培养企业开发真正需要的人才为目标,每个知识点都用案例来讲 ...
- jetbrick,新一代 Java 模板引擎,具有高性能和高扩展性
新一代 Java 模板引擎,具有高性能和高扩展性. <!-- Jetbrick Template Engineer --> <dependency> <groupId&g ...
- 微软“One Windows”的梦想已经破灭了吗?
导读 Windows 10 正式公布的时候,微软曾表示该系统将开启更为个性化的计算新纪元,可让用户在使用各类设备处理各项事务时,享受到一致.熟悉和可兼容的体验,从 Xbox 到 PC 和手机,再到平板 ...