Python 内部服务器错误 Flask

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24254945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:19:29  来源:igfitidea点击:

Internal server error Flask

pythonpython-2.7flask

提问by Liondancer

First time learning Flask and I am trying to build things following a tutorial. I am getting this message in my browser when I input this url:

第一次学习 Flask,我正在尝试按照教程构建东西。当我输入此 url 时,我在浏览器中收到此消息:

http://127.0.0.1:5000/index 

127.0.0.1 - - [16/Jun/2014 19:37:41] "GET /index HTTP/1.1" 500 -

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

I'm not sure why I am getting this error. Could someone help me out and tell me why? I am new to Flask and web development

我不确定为什么会收到此错误。有人可以帮助我并告诉我为什么吗?我是 Flask 和 Web 开发的新手

code:

代码:

from flask import Flask, request, make_response, redirect, render_template
from flask.ext.script import Manager
from flask.ext.bootstrap import Bootstrap


app = Flask(__name__)
manager = Manager(app)
bootstrap = Bootstrap(app)

@app.route('/index')
def index():
    return render_template('index.html')

@app.route('/user/<name>')
def user(name):
    return render_template('user.html', name = name)

if __name__ == '__main__':
    #app.run(debug = True)
    manager.run()

index.html:

索引.html:

{% extends "base.html" %}

{% block title %} Index {% block title %}

{% block head %}
    <!-- Uses super() to retain the original contents-->
    {{ super() }}
    <style type="text/css">

    </style>
{% endblock %}  
{% block body %}
<h1>Hello, World!</h1>
{% endblock %}

This is my project structure:

这是我的项目结构:

/Flask_0_11
   /templates
      base.html
      index.html
      user.html
   hello.py

采纳答案by Leonardo.Z

There's a template syntax error in your index.html.

您的index.html.

The title block should be closed with {% endblock %}:

标题栏应关闭{% endblock %}

{% block title %} Index {% endblock %}

You can turn on the DEBUGconfiguration for debugging. Because you use Flask-Script, you can pass the -doption to the runserver command.

您可以打开DEBUG配置进行调试。因为您使用Flask-Script,您可以将该-d选项传递给 runserver 命令。

e.g.

例如

python hello.py runserver -d

回答by Hiro

First of all try to run the app using

首先尝试使用运行该应用程序

python manage.py runserver -d

This will run your flask app in debug mode showing the errors encountered in your app making correction easily.

这将在调试模式下运行您的 Flask 应用程序,显示您的应用程序中遇到的错误,从而轻松进行更正。

Secondly,there may be error due to no WTF_CSRF_ENABLED = True with SECRET_KEY in your config file.

其次,可能由于配置文件中没有 WTF_CSRF_ENABLED = True 和 SECRET_KEY 导致错误。