Python系列视频教程: Django【13讲】第四讲 运算符-表达式

Python系列视频教程: Django【13讲】第四讲 运算符-表达式

 

step1:模板标签的使用

mytag.html

使用条件标签

<body>
{% if %}
{% else%}
{% endif %}

</body>

 

<body>
{% if user%}
<h1>name: {{user.name}}</h1>
{% else%}
user not exist用户不存在
{% endif %}

</body>

 

def mytag(req): 
  #user={'name':'tom','age':23,'sex':'male'}
  user=Person('Alexander',35,'male')
  book_list=['python','java','php','web']
  return render_to_response('mytag.html',{'title':'my page','book_list':book_list})

user not exist用户不存在

 

下面去查看api文档

https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#if

 

Use of both and andor clauses within the same tag is allowed, withand having higher precedence thanor e.g.:

{% if athlete_list and coach_list or cheerleader_list %}

 

will be interpreted like:

if (athlete_list and coach_list) or cheerleader_list

Use of actual parentheses in the if tag is invalid syntax. If you need them to indicate precedence, you should use nestedif tags.

 

 >  <  == !=  >= <=

 in

 not in

step2:使用for循环

{% for book in book_list %}
  <li>book:{{book}}</li>
{% endfor %}

user not exist用户不存在

the say:
  • book:python
  • book:java
  • book:php
  • book:web

     

    step3:对字典的遍历

    恢复user变量的传递

    原先的字典改名为user_dict,一起传递

    def mytag(req): 
      user_dict={'name':'tom','age':23,'sex':'male'}
      user=Person('Alexander',35,'male')
      book_list=['python','java','php','web']
      return render_to_response('mytag.html',{'title':'my page','user':user,'book_list':book_list,'user_dict':user_dict})

    mytag.html

    <body>
    {% if user%}
    <h1>name: {{user.name}}</h1>
    {% else%}
    user not exist用户不存在
    {% endif %}
    <div>
    the {{user.name}} say:{{user.say}}
    </div>
    {% for book in book_list %}
      <li>book:{{book}}</li>
    {% endfor %}
    {% for k in user_dict %}
      <li>user_dict:{{k}}</li>
    {% endfor %}
    </body>

    http://127.0.0.1:8000/blog/mytag/

     

    name: Alexander

    the Alexander say:I'm Alexander
  • book:python
  • book:java
  • book:php
  • book:web
  • user_dict:age
  • user_dict:name
  • user_dict:sex

     

    使用KV对

    {% for k,v in user_dict.items %}
      <li>{{k}}:{{v}}</li>
    {% endfor %}

    name: Alexander

    the Alexander say:I'm Alexander
  • book:python
  • book:java
  • book:php
  • book:web
  • age:23
  • name:tom
  • sex:male

    step4:for的进一步使用

    for

    The for loop sets a number of variables available within the loop:

    VariableDescription
    forloop.counterThe current iteration of the loop (1-indexed)
    forloop.counter0The current iteration of the loop (0-indexed)
    forloop.revcounterThe number of iterations from the end of the loop (1-indexed)
    forloop.revcounter0The number of iterations from the end of the loop (0-indexed)
    forloop.firstTrue if this is the first time through the loop
    forloop.lastTrue if this is the last time through the loop
    forloop.parentloopFor nested loops, this is the loop surrounding the current one

    {% for k,v in user_dict.items %}
      <li>{{forloop.counter}}.{{k}}:{{v}}</li>
    {% endfor %}

     

    name: Alexander

    the Alexander say:I'm Alexander
  • book:python
  • book:java
  • book:php
  • book:web
  • 1.age:23
  • 2.name:tom
  • 3.sex:male

    使用empty

    optional {% empty %} clause whose text is displayed if the given array is empty or could not be found:

    {% for k,v in user_dict.items %}
      <li>{{forloop.counter}}.{{k}}:{{v}}</li>
      {% empty %}
        <li>Sorry, no user in this list.</li>

    {% endfor %}

内容概要:本文介绍了如何使用Python识别图片和扫描PDF中的文字。首先,文章解了使用Spire.OCR for Python库来识别图片中的文字,包括安装库、配置OCR模型路径和语言设置、扫描图片以及保存识别后的文本。其次,详细描述了从图片中提取文字及其坐标位置的方法,使用户不仅能够获取文本内容,还能知道文本在图片中的具体位置。最后,文章还介绍了如何结合Spire.PDF for Python将PDF文件转换为图片格式,再通过OCR技术从中提取文字,适用于处理扫描版PDF文件。文中提供了完整的代码示例,帮助读者理解和实践。 适合人群:对Python编程有一定基础,希望学习或提高光学字符识别(OCR)技术的应用开发者,尤其是需要处理大量图片或PDF文档中文字信息的工作人员。 使用场景及目标:① 开发者可以利用这些方法自动化处理图片或PDF文档中的文字信息,提高工作效率;② 实现从非结构化数据(如图片、扫描件)到结构化数据(如文本文件)的转换,便于后续的数据分析和处理;③ 提供了一种解决纸质文档数字化的有效途径,特别是对于历史档案、书籍等资料的电子化保存。 其他说明:需要注意的是,OCR的准确性很大程度上取决于图片的质量,清晰度高、对比度好的图片可以获得更好的识别效果。此外,不同OCR库可能对特定语言或字体的支持程度不同,选择合适的库和配置参数能显著提升识别精度。在实际应用中,建议先进行小规模测试,优化参数后再大规模应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值