[英]Flask is returning TypeError: The view function did not return a valid response
I am new to Flask and trying to build simple userform page using flask and running into error. 我是Flask的新手,并尝试使用flask建立简单的用户窗体页面,并遇到错误。 When I am trying to render just pure HTML, extending base.html, everything works perfectly, however when added macro, result is below.
当我尝试仅渲染纯HTML并扩展base.html时,一切工作正常,但是添加宏后,结果如下。 I tried everything and cannot solve the issue on my own.
我尝试了所有事情,但无法独自解决问题。 Any hints, what am I doing wrong?
有什么提示,我在做什么错?
[2019-07-09 22:19:39,837] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "path/venv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "path/venv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "path/venv/lib/python3.6/site-packages/flask/app.py", line 1967, in finalize_request
response = self.make_response(rv)
File "path/venv/lib/python3.6/site-packages/flask/app.py", line 2130, in make_response
" {rv.__class__.__name__}.".format(rv=rv)
TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a UserForm.
127.0.0.1 - - [09/Jul/2019 22:19:39] "GET / HTTP/1.1" 500 -
This is my Python code: 这是我的Python代码:
from app import app
from flask import render_template
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SelectField
from wtforms.validators import InputRequired
@app.route('/', methods=['GET', 'POST'])
class UserForm(FlaskForm):
prefix = StringField("Prefix", validators=[InputRequired()])
anrede = StringField("Anrede", validators=[InputRequired()])
def form():
form = UserForm()
return render_template('html_one_flask.html', form=form)
form which I am trying to load 我要加载的表格
{% extends "base.html" %}
{% from "render_field.html" import render_field %}
{% block content %}
<div class = "container">
<div class = "spacer" > </div>
</div>
<div class = "container" id = "data_category">PERSONAL DATEN</div>
<div class = "container">
<div class = "spacer" > </div>
<div class="form-group row flex-v-center" id = 1>
{{ render_field(form.prefix), placeholder = form.prefix.label.text }}
{{ render_field(form.anrede), placeholder = form.anrede.label.text }}
</div>
</div>
{% endblock %}
and macro imported from another file 和从另一个文件导入的宏
{% macro render_field(field) %}
<div class = "col-0.75">
<div class = "spacer_label" > </div>
<label for="{{ field.name }}">{{ field.label.text }} </label>
</div>
<div class = "col-1">
{{ field(class_ = "form-control input-sm", **kwargs|safe }}
</div>
{% endmacro %}
Your @app.route
should be above the view function, not the class declaration: 您的
@app.route
应该位于view函数上方,而不是类声明:
@app.route('/', methods=['GET', 'POST'])
def form():
...
您将@ app.route装饰器放在了黄色的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.