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.

django.core.urlresolvers

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的更多相关文章

  1. Django views 中 View decorators

    decorators(装饰器) 1. require_http_methods 在django.views.decorators.http中,可以用来限制请求的权限. require_http_met ...

  2. django views视图函数返回值 return redirect httpresponse总结

    django views视图函数返回值 return redirect  render httpresponse总结

  3. Django Views and URLconfs

    碧玉妆成一树高,万条垂下绿丝绦. 不知细叶谁裁出,二月春风似剪刀. 原文尽在:http://djangobook.com/ 转载请注明出处:http://www.cnblogs.com/A-FM/p/ ...

  4. django views视图函数

    Django views.py视图文件 一. 创建views.py文件,在工程文件夹根目录创建views.py视图文件,其实任意文件名都可以,使用views是为了遵循传统. 注:所有的views函数都 ...

  5. Django Views Decorator

    Django的试图函数的装饰器主要有: HTTP请求方法 条件视图处理 GZip压缩 改变页眉 缓存 官网文档 HTTP请求方法 该装饰器是设置允许访问HTTP协议的方法,装饰器在django.vie ...

  6. Django—views系统:views基础

    Django的View(视图)简介 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错 ...

  7. Django -- Views and URLconf

    1 创建工程 django-admin startproject mysite && cd mysite 2 创建应用 python manage.py startapp blog 3 ...

  8. Django Views(视图函数)

    http请求中产生两个核心对象: http请求:HttpRequest对象 http响应:HttpResponse对象 所在位置:django.http 之前我们用到的参数request就是HttpR ...

  9. django views视图

    视图函数简称视图,本质上是一个简单的python函数,它接受web请求并且返回web响应:响应的内容可以是HTML网页.重定向.404错误.XML文档或图像等任何东西,但是,无论视图本身是个什么处理逻 ...

随机推荐

  1. 基本linux命令

    1.mkdir mkdir 创建目录   mkdir -p 循环创建目录 2.cd     切换目录 3.pwd   查看当前路径 4.mkdir 删除一个空的目录 5.cp 复制文件/目录  -r用 ...

  2. gspx请求周期(备忘)

  3. Windows Phone 开发环境的搭建

    1. 系统 系统:Windows 7(32 位).Windows 7(64 位).Windows Vista SP2(32 位)和 Windows Vista(64 位)或者更高版本. 不支持 :Wi ...

  4. 2013nanjingJ

    J - Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. solrcloud

    @Test public void querySolrCloud(){ String zkHost = "127.0.0.1:2181"; String defaultCollec ...

  6. C#GDI+基础(三)画刷详解

    SolidBrush:一般的画刷,通常只用一种颜色去填充GDI+图形 创建一般画刷: SolidBrush sbBrush1 = new SolidBrush(Color.Green); HatchB ...

  7. JS获取地址栏参数

    获取地址栏参数(其中name为你所需要的参数值) function GetQueryString(name) { var reg = new RegExp("(^|&)" ...

  8. mybaitis uuid插入和定义返回类型

  9. iOS开发——UI进阶篇(十六)Quartz2D实战小例子

    一.画线 只有在drawRect中才能获取到跟view相关联的上下文 - (void)drawRect:(CGRect)rect {} 一条线 // 1.获取跟当前View相关联的layer上下文(画 ...

  10. unity3d教程游戏包含的一切文件导入资源

    http://www.58player.com/blog-2327-954.html 导入资源 将文件增加至工程文件夹的资源 (Assets) 文件夹后,Unity 将自动检测文件.将任何资源 (As ...