简体   繁体   English

Flask:如何在基本jinja2模板中放置表单?

[英]Flask: How do I place Forms in Base jinja2 Template?

I am new to Flask and am writing a simple Flask application with search facility. 我是Flask的新手,正在用搜索工具编写一个简单的Flask应用程序。 Similar to google search, I want to retain the search input box in the search result page while showing the results.So, I placed search form in the base template and a derived search result template from the base template to show both search form and search results. 与google搜索类似,我希望在显示结果时保留搜索结果页面中的搜索输入框。因此,我将搜索表单放置在基本模板中,并从基本模板派生了搜索结果模板,以同时显示搜索表单和搜索结果。

For this, I did the following:- 为此,我做了以下事情:

A Base Template(base.html) with Page metadata and Form(which should basically be present even in the result page). 具有页面元数据和表单(即使在结果页面中也应基本存在)的基本模板(base.html)。

<html>
  <head>
    {% if title %}
    <title>{{title}}</title>
    {% else %}
    <title>Search</title>
    {% endif %}
  </head>
  <body>
    <h1>Search </h1>
<form action="/series_search" method="post" name="search">
    {{form.hidden_tag()}}
    <p>
        Please enter query: <br>
        {{form.search(size=10)}}
    </p>
    <p><input type="submit" value="Search"></p>
</form>
<br>
{% block content %}{% endblock %}

  </body>
</html>

The derived template(derived.html) has the following code, which is inhering the base template(which has the search template): 派生的模板(derived.html)具有以下代码,该代码继承了基本模板(具有搜索模板):

{% extends "base.html" %}

{% block content %}
<h1>Search Result</h1>
    {% if result %}
    <p> Title: {{result.title}}</p>
    {% else %}
    <p> search not found!!</p>
    {% endif %}
{% endblock %}

And, In the view, the following 并且,在视图中,以下内容

@app.route('/search', methods = ['POST', 'GET'])
def search():
    form = SearchForm()
    if form.validate_on_submit():
        print "form validated"
        query = form.search.data
        result = Lib.get_result(query)
        return render_template('derived.html', result = result)
    return render_template('search.html',
    title = 'Search',
    form = form)

If I enter the query and submit the search form, I get the following error, 如果我输入查询并提交搜索表单,则会收到以下错误消息:

....
....
File "/Users/webapp/app/templates/derived.html", line 1, in top-level template code
    {% extends "search.html" %}
  File "/Users/webapp/app/templates/search.html", line 12, in top-level template code
    {{form.hidden_tag()}}
  File "/Users/webapp/flask/lib/python2.7/site-packages/jinja2/environment.py", line 397, in getattr
    return getattr(obj, attribute)

So, Basically after the submit button is pressed, the view renders the derived.html which is derived from base.html. 因此,基本上,在按下“提交”按钮之后,视图将呈现派生自base.html的派生的.html。 The base.html has a form which should be set. base.html具有应设置的形式。

  1. How do I pass the form object of base template while calling the derived template? 调用派生模板时如何传递基本模板的表单对象?
  2. Is my approach right to what I want to achieve? 我的方法适合我想要实现的目标吗? If there are any other, please suggest. 如果还有其他问题,请提出建议。

Thanking you in advance 预先谢谢你

You just need to pass the form keyword argument to your other render_template call. 您只需要将form关键字参数传递给其他render_template调用。

return render_template('derived.html', result=result)

should instead be: 相反,应为:

return render_template('derived.html', result=result, form=form)

The reason this is needed is because the web is stateless - the rendered template is not preserved on Flask's side. 之所以需要这样做,是因为Web是无状态的-呈现的模板未保留在Flask的一侧。 The HTML is sent to the client and then everything is done, as far as Flask is concerned. 将HTML发送到客户端,然后就Flask而言,一切都已完成。 The POST request is a separate request from the server's perspective and the only thing that is called is the render_template('derived') call. 从服务器的角度来看,POST请求是一个单独的请求,唯一被调用的是render_template('derived')调用。 There is no "memory" of having created a form and sent it to search.html . 创建表单并将其发送到search.html并没有“记忆”。

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

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