简体   繁体   English

你如何在烧瓶模板中声明 python 变量?

[英]How do you declare python variables within flask templates?

I've got some basic experience building websites using a LAMP stack.我有一些使用 LAMP 堆栈构建网站的基本经验。 I've also got some experience with data processing using Python.我也有一些使用 Python 处理数据的经验。 I'm trying to get a grip on the mongodb-flask-python thing, so I fired everything up using this boilerplate: https://github.com/hansonkd/FlaskBootstrapSecurity我试图掌握 mongodb-flask-python 的事情,所以我使用这个样板启动了一切: https : //github.com/hansonkd/FlaskBootstrapSecurity

All is well.一切都很好。

To experiment, I tried declaring a variable and printing it...为了实验,我尝试声明一个变量并打印它......

I get this error message:我收到此错误消息:

TemplateSyntaxError: Encountered unknown tag 'x'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.

Here's my main index.html page这是我的主要 index.html 页面

{% extends "base.html" %}


{% block content %}

    <div class="row">
        <div class="col-xs-12">
            Hello World, at {{ now }}, {{ now|time_ago }}
        </div>
    </div>
    <div class="row-center">
        <div class="col">
            {% x = [0,1,2,3,4,5] %}
            {% for number in x}
            <li> {% print(number) %}
            {% endfor %}
        </div>
    </div>

{% endblock %}

I love learning new things, but man, can I ever get hung up for hours on the simplest of things... any help would be greatly appreciated!!!我喜欢学习新事物,但是伙计,我能不能在最简单的事情上挂断几个小时……任何帮助将不胜感激!!!

Flask uses Jinja as its default templating engine. Flask 使用 Jinja 作为其默认模板引擎。

The templating language is python-esque, but is not python.模板语言是 python 式的,但不是 python。 This is different from something like a phtml file, which is php interspersed with html.这与 phtml 文件之类的东西不同,它是用 html 穿插的 php。

Check the jinja documentation for more of what you can do, but here's how you set a variable within a template:查看 jinja 文档以了解更多您可以执行的操作,但这里是您在模板中设置变量的方法:

{% set x = [0,1,2,3,4,5] %}

http://jinja.pocoo.org/docs/2.9/templates/#assignments http://jinja.pocoo.org/docs/2.9/templates/#assignments

Try this:试试这个:

{% set x = [0,1,2,3,4,5] %}

See Jinja docs .请参阅 Jinja 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM