简体   繁体   中英

Internal Server Error when trying to run flask

I am trying to create a website with a web form in it using flask, but I keep getting an Internal Server error, despite getting no error when I run it in the console

Here is my code:

__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>

When I run flask run I get this

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

When I try to access http://127.0.0.1:5000/ in both chrome and firefox I get an error, but I feel like that's a whole separate question.

If all of your codes are here then I can tell you the problem might be about rendering form, try this one ( pip install flask-bootstrap ):

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

main.html

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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