html - Python语法问题

查看:118
本文介绍了html - Python语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在HTML文件中总提示{%for i in range(10)%} 错误,请问是什么原因?

            {% for a in range(10) %}#此处出错
            <div class="fh5co-entry padding">
                <img src="/blogjs/images/project-1.jpg" alt="Free HTML5 Bootstrap Template by FreeHTML5.co">
                <div>
                    <span class="fh5co-post-date">October 12, 2016</span>
                    <h2><a href="/blogjs/single.html">How to be an effective web developer</a></h2>
                    <p>How two simple exercises changed my life</p>
                </div>
            </div>
            {% endfor %}

错误提示如下:

浏览器错误提示如下:

TemplateSyntaxError at /index/

Could not parse the remainder: '(10)' from 'range(10)'

Request Method:     GET
Request URL:     http://localhost:8000/index/
Django Version:     1.11.4
Exception Type:     TemplateSyntaxError
Exception Value:     

Could not parse the remainder: '(10)' from 'range(10)'

Exception Location:     /Library/Python/2.7/site-packages/django/template/base.py in parse, line 515
Python Executable:     /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version:     2.7.10
Python Path:     

['/Users/eric/Desktop/sundyblog',
 '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg',
 '/Users/eric/Desktop/sundyblog',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/Library/Python/2.7/site-packages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

Server time:     Sat, 12 Aug 2017 13:53:15 +0000

解决方案

django的模板中并不支持range
如果你需要使用的话的几种方式实现
1.for i in "xxxxx"要几次就写几个x
2.在views里返回range
3.使用自定义filter

这篇关于html - Python语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆