zoukankan      html  css  js  c++  java
  • django开发框架-view & template

    django框架的主要模型是MVT,Model模型,View视图,Template模板,基于基本的HttpRequest方式。

    django支持的数据库有四种:PostgreSQL,MySQL, Oracle,SQLite3。(所以目前应该把精力放在PostgreSQL上)

    1.新建工程:django-admin.py startproject mysite,新建的mysite的目录下有四个文件:__init__.py, manage.py, settings.py, urls.py 。

    2.运行开发服务器:python manage.py runserver。(这是内建的、轻量的web服务)(如何非本地IP访问?)

    3.

    4.

    5.

    6.

    7.

    8.

    9.(忘记保存了,我草)

    10.模板的深度变量查找:使用句点符号 . 。据点查找规则:字典类型查找,属性查找,方法调用,列表类型索引查找。设置函数属性function.alters_data = true ,在模板载入时,方法就不会执行。如果变量不存在,就处理成空字符串。context对象可以像字典一样insert, delete。

    11.模块标签:if/else {% if bool %} ... {% else %} ... {% endif %}, if标签不允许在同一个标签中使用 and 和 or ,不支持用圆括号来组合比较操作。for {% for athlete in athlete_list %} ... {% endfor %} ,for 标签还有一个可选的 {% empty %}标签,当列表为空时使用。有一个forloop的模板变量。ifequal/ifnotequal 标签,只有模板变量,字符串,整数和小数可以作为该标签的参数。注释 {# comment #} ,多行注释 {% comment %} ... {% endcomment %}。 过滤器使用管道字符,可以套接,过滤器参数跟随冒号之后并且总是用双引号包含。

    1 def current_time(request):                                                  |   ~                          
    2  10     now = datetime.datetime.now()                                           |   ~                          
    3  11     t = get_template('mytemplate.html')                                     |   ~                          
    4  12     html = t.render(Context({'current_date': now}))                         |   ~                          
    5  13     return HttpResponse(html)

    12.先在settings.py中设置template_dirs,然后编写html代码,再到views.py中编写对应的视图代码。

    1 def current_time(request):
    2     now = datetime.datetime.now()
    3     return render_to_response('mytemplate.html', {'current_date': now})

    另一种写法:使用python的内建函数locals()

    def current_time(request):
        current_date = datetime.datetime.now()
        return render_to_response('mytemplate.html', locals())

    get_template()函数可以使用子目录。

    13.include模板标签。模板继承,代码示例:

    base.html:

     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
     2 <html lang="en">
     3 <head>
     4     <title> {% block title %}{% endblock %} </title>
     5 </head>
     6 <body>
     7     <h1> My helpful timestamp site </h1>
     8     {% block content %}{% endblock %}
     9     {% block footer %}
    10     <hr>
    11     <p>Thank for visiting my site.</p>
    12     {% endblock %}
    13 </body>
    14 </html>

    current_date.html:

    1 {% extends "base.html" %}
    2 
    3 
    4 {%block title%}The current time{% endblock %}
    5 
    6 {%block content%}
    7 <p>It is now {{ current_date }}.</p>
    8 {% endblock %}

    简单的视图模式和模板模式介绍完毕。

  • 相关阅读:
    codeforces 652B z-sort(思维)
    poj 3268 Silver Cow Party(最短路)
    POJ 2243:Knight Moves(BFS)
    POJ 1107:W's Cipher(模拟)
    POJ 1008 Maya Calendar(模拟)
    Hdu3436-Queue-jumpers(伸展树)
    主席树的另一种写法
    Hdu5785-Interesting(回文串处理)
    Hdu5008-Boring String Problem(后缀数组)
    RMQ模板
  • 原文地址:https://www.cnblogs.com/nuaalida/p/4338244.html
Copyright © 2011-2022 走看看