render_template with multiple variables

前端 未结 3 596
再見小時候
再見小時候 2020-12-13 09:23

I am using Flask(as framework) and MongoDB(as database server). Right now, all i can do is just pass one argument that i got from the database:

@app.route(\'         


        
相关标签:
3条回答
  • 2020-12-13 09:45
    return render_template('im.html', user= None, content = xxx, timestamp = xxx)
    

    You can pass as many variables as you need. The api

    excerpt:

    flask.render_template(template_name_or_list, **context) Renders a template from the template folder with the given context.

    Parameters: template_name_or_list – the name of the template to be rendered, or an iterable with template names the first one existing will be rendered context – the variables that should be available in the context of the template.

    0 讨论(0)
  • 2020-12-13 09:50

    You can pass multiple parameters to the view.

    You can pass all your local variable

    @app.route('/')
    def index():
      content = """
         teste
       """
      user = "Hero"
      return render_template('index.html', **locals())
    

    or just pass your data

    def index() :
        return render_template('index.html', obj = "object", data = "a223jsd" );
    

    api doc

    0 讨论(0)
  • 2020-12-13 09:50

    It is also possible to pass a list to render_template's context variables, and refer to its elements with Jinja's syntax in HTML.

    example.py

    l = [user, content, timestamp]
    return render_template('exemple.html', l=l)
    

    exemple.html

    ...
    <body>
        {% for e in l %}
            {{e}}
        {% endfor %}
    </body>
    ...
    
    0 讨论(0)
提交回复
热议问题