简体   繁体   English

如何将flask wtform从扩展视图移动到扩展视图并实例化所有视图的表单?

[英]How to move flask wtform from extending view to extended view and instantiate forms for all views?

To make my question clearer, here is a little application that takes a sentence input and outputs that sentence twice. 为了使我的问题更清楚,这里有一个小应用程序,它接受句子输入并输出该句子两次。

I have base.html : 我有base.html

<html>
  <head>
    <title> My site </title>
    <body>
    {% block content %}{% endblock %}
   </body>
</html>

and index.html : index.html

{% extends "base.html" %}
{% block content %}
{{ s }}
<form action="" method="post" name="blah">
    {{ form.hidden_tag() }}
    {{ form.sentence(size=80) }}
    <input type="submit" value="Doubler"></p>
</form>
{% endblock %}

Here is part of views.py : 这是views.py一部分:

from forms import DoublerForm

@app.route('/index')
def index():
    form = DoublerForm()
    if form.validate_on_submit():
        s = form.sentence.data
        return render_template('index.html', form=form, s=str(s) + str(s))
    return render_template('index.html', form=form, s="")

And here is forms.py , without all the imports: 这里是forms.py ,没有所有导入:

class DoublerForm(Form):
    sentence = StringField(u'Text')

This seems to work OK. 这似乎工作正常。 But what I would like is to have my input form in the base.html template so that this shows up on all pages that extend it, not just the index page. 但我想要的是在base.html模板中输入我的输入表单,以便它显示在扩展它的所有页面上,而不仅仅是index页面。 How can I move the form to the base.html and instantiate the form for all views that extend base.html? 如何将表单移动到base.html并为扩展base.html的所有视图实例化表单?

You can use the flask.g object and flask.before_request . 您可以使用flask.g对象和flask.before_request

from flask import Flask, render_template, g
from flask_wtf import Form
from wtforms import StringField

@app.before_request
def get_default_context():
    """
    helper function that returns a default context used by render_template
    """
    g.doubler_form = DoublerForm()
    g.example_string = "example =D"

@app.route('/', methods=["GET", "POST"])
def index():
    form = g.get("doubler_form")
    if form.validate_on_submit():
        s = form.sentence.data
        return render_template('index.html', form=form, s=str(s) + str(s))
    return render_template('index.html', form=form, s="")

You can also explicitly define a context function 您还可以显式定义上下文函数

def get_default_context():
    """
    helper function that returns a default context used by render_template
    """
    context = {}
    context["doubler_form"] = form = DoublerForm()
    context["example_string"] = "example =D"
    return context

and is used like this 并像这样使用

@app.route('/faq/', methods=['GET'])
def faq_page():
    """
    returns a static page that answers the most common questions found in limbo
    """
    context = controllers.get_default_context()
    return render_template('faq.html', **context)

Now, you'll have whatever objects you add to the context dictionary available in all templates that unpack the context dictionary. 现在,您将拥有在解压缩上下文字典的所有模板中可用的上下文字典中添加的任何对象。

index.html 的index.html

{% extends "base.html" %}
{% block content %}
{{ s }}
{% endblock %}

base.html base.html文件

<html>
  <head>
    <title> My site </title>
    <body>
    {% block content %}{% endblock %}
    <form action="" method="post" name="blah">
      {{ doubler_form.hidden_tag() }}
      {{ doubler_form.sentence(size=80) }}
      {{ example_string }}
      <input type="submit" value="Doubler"></p>
    </form>
   </body>
</html>

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

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