zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    .net ajax和后台数据简单交互。传参JSON返回
    基于layui的管理后台简单导航,简单框架布局。左侧菜单栏以及动态操作tab项
    .net使用layui框架下。绑定搜索下拉框-模糊查询
    .NET用样式做模式模态窗口层,弹出遮罩层
    .net里ajax调用后台方法返回LIST集合
    sql的行转列(case when) sqlserver.net
    查SQLSERVER MSSQL查约束是哪张表
    powerbuilder 在pb里面怎么把文件编译成exe文件
    powerbuilder datawindow给表格某列赋值或设置禁用
    powerbuilder给文本框赋值
  • 原文地址:https://www.cnblogs.com/ybf-yyj/p/8120151.html
Copyright © 2011-2022 走看看