尝试运行flask

时间:2016-11-26 05:20:26

标签: python html flask

我正在尝试使用flask创建一个带有Web表单的网站,但我不断收到内部服务器错误,尽管在控制台中运行时没有出现错误

这是我的代码:

__ init.py

from flask import Flask, render_template
from forms import TestForm

app = Flask(__name__)
app.config.from_pyfile('config.py')

@app.route('/')
def homepage():
    return render_template("main.html", form=TestForm())



if __name__ == "__main__":
    app.run(debug=True)

forms.py

from flask_wtf import Form
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired

class TestForm(Form):
    test_form = StringField('test_form', validators=[DataRequired()])

config.py

WTF_CSRF_ENABLED = True
SECRET_KEY = '<super-secret>'

main.html中

<DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Sample Page</title>
    <meta name="viewport" content="width=device-width"
    initial=scale=1/>
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='favicon.ico') }}" rel="shortcut icon">
</head>
<h2>Hello, this site is meant to test my fill_web_form.py script</h2>
<br>
<br>
<h1>Test Form</h1>
<form action="/" method="post" name="login">
      {{ render_field(form.test_form(size=80) }}<br>
      <p><input type="submit" value="Sign In"></p>
 </form>
</html>

当我运行flask run时,我得到了这个

$ flask run
  * Serving Flask app "FlaskApp"
  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

当我尝试在chrome和firefox中访问http://127.0.0.1:5000/时出现错误,但我觉得这是一个完全不同的问题。

1 个答案:

答案 0 :(得分:1)

如果你的所有代码都在这里,那么我可以告诉你问题可能是关于渲染表单,试试这个(pip install flask-bootstrap):

# __init.py
...
bootstrap = Bootstrap(app)

main.html中

{% import 'bootstrap/wtf.html' as wtf %}
...
{{ wtf.quick_form(form) }}
...