• django第三课 模版


    第一步 创建项目文件:

        django-admin.py startproject *** 

    第二步 进入该文件下创建文件夹templates,在该文件夹下创建thanks.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Odering Notice</title>
    </head>
    <body>
    
    <h1>Ordering Notice</h1>
    
    <p>Dear {{person_name}},</p>
    
    <P>Thanks for placing an oder form {{company}}.It's scheduled
    ship on {{ship_date|date:"F j,Y"}}</P>
    
    <P>Here are the items you've order:</P>
    
    <U>
        {% for item in item_list %}
        <li>{{item}}</li>
        {% endfor %}
    </U>
    
    {% if ordered_warranty %}
        <p>Your warranty information will be included in tne packaging. </p>
    {% else %}
        <p>You didn't order a warranty, so you're on your own when
        the products inevitably stop working.</p>
    {% endif %}
    
    <p>Sincerely,<br/>{{company}}</p>
    
    </body>
    </html>

    第三步 修改settings.py

    第四步 新建views.py(与settings.py处于同一文件夹下)

    from django.http import HttpResponse,Http404
    import datetime
    from django.shortcuts import render_to_response
    
    def thanks(request):
        return render_to_response("1.html",{'person_name':'ABC','company':'ABC','ship_date':datetime.date(2017,12,24),'ordered_warranty':True})

    第五步 修改urls.py(这里我修改了代码,红色部分)

    from django.conf.urls import url
    from myThirdproject.views import current_datetime,hours_ahead,hello,thanks,current_datetime1,hours_ahead1
    
    urlpatterns = [
        url(r'^thanks/$', thanks),
    ]

    第六步 启动server

      python manage.py runserver

    OK 搞定!!!!

    以上介绍了django的简单数据绑定,详细内容大家可以百度文档,下面来介绍一下django是如何实现以上功能的,首先,windows键+R进入DOS,输入:python manage.py shell,进入python交互界面(注意如果要在pycharm里面运行还要配置环境变量比较麻烦),输入以下:

    >>> from django.template import Template, Context
    >>> raw_template = """<p>Dear {{ person_name }},</p>
    ...
    ... <p>Thanks for placing an order from {{ company }}. It's scheduled to
    ... ship on {{ ship_date|date:"F j, Y" }}.</p>
    ...
    ... {% if ordered_warranty %}
    ... <p>Your warranty information will be included in the packaging.</p>
    ... {% else %}
    ... <p>You didn't order a warranty, so you're on your own when
    ... the products inevitably stop working.</p>
    ... {% endif %}
    ...
    ... <p>Sincerely,<br />{{ company }}</p>"""
    >>> t = Template(raw_template)
    >>> import datetime
    >>> c = Context({'person_name': 'John Smith',
    ... 'company': 'Outdoor Equipment',
    ... 'ship_date': datetime.date(2009, 4, 2),
    ... 'ordered_warranty': False})
    >>> t.render(c)
    u"<p>Dear John Smith,</p> <p>Thanks for placing an order from Outdoor
    Equipment. It's scheduled to ship on April 2, 2009.</p> <p>You
    didn't order a warranty, so you're on your own when the products
    inevitably stop working.</p> <p>Sincerely,<br />Outdoor Equipment
    </p>"

    下面来解释一下结果为什么是这样:

      首先我们导入 (import)类 Template 和 Context ,它们都在模块 django.template 里。
      我们把模板原始文本保存到变量 raw_template 。注意到我们使用了三个引号来 标识这些文本,因为这样可以包含多行。
      接下来,我们创建了一个模板对象 t ,把 raw_template 作为 Template 类构造函数的参数。我们从Python的标准库导入 datetime 模块,以后我们将会使用它。
      然后,我们创建一个 Context 对象, c 。 Context 构造的参数是Python 字典数据类型。 在这里,我们指定参数 person_name 的值是 'John Smith', 参数company 的值为‘Outdoor Equipment’ ,等等。
      最后,我们在模板对象上调用 render() 方法,传递context参数给它。 这是返回渲染后的模板的方法,它会替换模板变量为真实的值和执行块标签。
      注意,warranty paragraph显示是因为 ordered_warranty 的值为 True. 注意时间的显示, April 2, 2009, 它是按 'F j, Y' 格式显示的。

    一旦有了 模板对象,你就可以通过它渲染多个context, 例如:
    >>> from django.template import Template, Context
    >>> t = Template('Hello, {{ name }}')
    >>> print t.render(Context({'name': 'John'}))
    Hello, John
    >>> print t.render(Context({'name': 'Julie'}))
    Hello, Julie
    >>> print t.render(Context({'name': 'Pat'}))
    Hello, Pat

  • 相关阅读:
    (转) 将VB.NET网站转换成C#的全过程
    vb.net转换为C#方法
    (转)使用Microsoft Web Application Stress Tool对web进行压力测试
    (转)js 中{},[]中括号,大括号使用详解
    (转)js学习笔记()函数
    (转)几种HtmlEncode的区别
    编译Redis系统提示缺少gcc,可以使用yum进行安装:
    linux如何关闭防火墙
    Linux less命令简介
    Linux unzip解压文件到某个目录下面
  • 原文地址:https://www.cnblogs.com/ybf-yyj/p/8120151.html
Copyright © 2020-2023  润新知