zoukankan      html  css  js  c++  java
  • 在 Django 模板中遍历复杂数据结构的关键是句点字符

    在 Django 模板中遍历复杂数据结构的关键是句点字符 ( . )。

    实例二 mysit/templates/myhtml2.html修改如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {{ time.year }}年{{ time.month }}月{{ time.day }}日 </body> </html> mysit/blog/views.py修改如下: from django.shortcuts import render, HttpResponse, redirect import datetime def myhtml2(request): # 方法一 # 获取当前时间对象,有属性.year .month .day 分别获取年月日 # HTML中获取数据 根据 {{ time.year }}年{{ time.month }}月{{ time.day}}日 # time = datetime.datetime.now() # 方法二 # 获取当前时间对象,分别获取年月日添加到列表中 # HTML中获取数据 根据 {{ time.0 }}年{{ time.1 }}月{{ time.2}}日 # time = [ datetime.datetime.now().year, datetime.datetime.now().month, # datetime.datetime.now().day] # 方法三 # 获取当前时间对象,分别获取年月日添加到字典中 # HTML中获取数据 根据 {{ time.year }}年{{ time.month }}月{{ time.day}}日 time = {"year": datetime.datetime.now().year, "month": datetime.datetime.now().month, "day": datetime.datetime.now().day} return render(request, "myhtml2.html", locals()) 所以在模板语言中句点(.)可以获取任意对象的任意属性 变量过滤器 # 语法格式: {{obj|filter:param}} # value1 = "aBcDe" # HTML代码: value1|upper # 执行结果: ABCDE # value3 = 'he llo wo r ld' # HTML代码: value3|cut:'he' # 执行结果: llo wo r ld # date 格式化日期字符串 # value4 = datetime.datetime.now() # HTML代码: value4|date:'Y-m-d' # 执行结果: 2016-11-29 # value5 = [] # HTML代码: value5|default:'空的' # 执行结果: 空的 # value6 = '<a href="#">跳转</a>' # HTML代码: value6|safe # 执行结果: 跳转 # HTML代码: value6|striptags # 执行结果: 跳转 # value7 = '1234' # HTML代码: value7 | filesizeformat # 执行结果: 1.2 KB # HTML代码: value7 | first # 执行结果: 1 # HTML代码: value7 | length # 执行结果: 4 # HTML代码: value7 | slice: ":-1" # 执行结果: 123 # value8 = 'http://www.baidu.com/?a=1&b=3' # HTML代码: value8|urlencode # 执行结果: http%3A//www.baidu.com/%3Fa%3D1%26b%3D3 模板标签 标签(tag)的使用(使用大括号和百分比的组合来表示使用tag) {% tag %} ------------------------{%url "name" %}:引用路由配置的地址 <form action="{% url "aaa"%}" > #代表该表单提交的数据会交给 urls.py中 别名为 “aaa” 所对应的视图函数去执行 <input type="text"> <input type="submit"value="提交"> {%csrf_token%} </form> ------------------------{%csrf_token%}:用于生成csrf_token的标签,用于防治跨站攻击验证。注意如果你在视图函数中用的是render_to_response返回页面的方法,则该标签不会生效

    其实,这里是会生成一个input标签,和其他表单标签一起提交给后台的。

    实例三

    在实例一的基础上

    mysit/templates/myhtml2.html修改如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action={% url "aaa" %} method="post"> # 在urls.py中找到别名为“aaa”所对应的函数提交数据 <input type="submit"> </form> </body> </html> mysit/blog/urls.py from django.conf.urls import url from blog import views urlpatterns = [ url(r'index2/$', views.myhtml2, name="aaa"), # 所有blog开头的网址都会找到该py 网址最后是index2881064151 则对应views.myhtml2函数,提交表单,因为其别名为aaa,所以提交表单时执行views.myhtml2函数 ]

    运行,点击提交按钮你会发现以下错误

    Forbidden (403) CSRF verification failed. Request aborted. Help Reason given for failure: CSRF token missing or incorrect. In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure: Your browser is accepting cookies. The view function passes a request to the template's render method. In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL. If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data. The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login. You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed. You can customize this page using the CSRF_FAILURE_VIEW setting.

  • 相关阅读:
    如何在自定义端口上运行 Spring Boot 应用程序?
    FileUpload拦截器
    UI标签
    OGNL
    你对测试最大的兴趣在哪里?为什么?
    举例说明同步和异步。
    git 克隆分支
    JQuery 选择器
    JQuery对象和Dom对象
    简单的JQuery之Ready
  • 原文地址:https://www.cnblogs.com/cbryge/p/6189274.html
Copyright © 2011-2022 走看看