【问题标题】:Getting internal server error 500 while displaying output in Flask在 Flask 中显示输出时出现内部服务器错误 500
【发布时间】:2017-06-25 15:20:57
【问题描述】:

我刚刚开始学习 Flask,并参考了一个教程来运行一小段测试代码。

代码如下:

Hello.py

from flask import Flask, render_template, request
app = Flask(__name__)

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

@app.route('/result',methods = ['POST', 'GET'])
def result():
   if request.method == 'POST':
      result = request.form
      return render_template("result.html",result = result)

if __name__ == '__main__':
   app.run()

student.html

<html>
   <body>

      <form action = "http://localhost:5000/result" method = "POST">
         <p>Name <input type = "text" name = "Name" /></p>
         <p>Physics <input type = "text" name = "Physics" /></p>
         <p>Chemistry <input type = "text" name = "chemistry" /></p>
         <p>Maths <input type ="text" name = "Mathematics" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>

   </body>
</html>

result.html

<!doctype html>
<html>
   <body>

      <table border = 1>
         {% for key, value in result.iteritems() %}

            <tr>
               <th> {{ key }} </th>
               <td> {{ value }} </td>
            </tr>

         {% endfor %}
      </table>

   </body>
</html>

我将student.htmlresult.html 保存在templates 文件夹中。在运行Hello.py 时,student.html 运行并显示一个表单,我可以在其中输入字段中的值。单击提交应该会显示result.html 页面,但它会给出以下错误:

内部服务器错误 服务器遇到内部错误,无法完成您的请求。要么服务器超载,要么应用程序出错。

追溯:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [25/Jun/2017 20:48:15] "GET / HTTP/1.1" 200 -
[2017-06-25 20:48:24,634] ERROR in app: Exception on /result [POST]
Traceback (most recent call last):
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "<ipython-input-1-0b3c292b941b>", line 12, in result
    return render_template("result.html",result = result)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\templating.py", line 134, in render_template
    context, ctx.app)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\flask\templating.py", line 116, in _render
    rv = template.render(context)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\jinja2\environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\Kshitiz\Anaconda3\lib\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Kshitiz\Desktop\doc2vec\templates\result.html", line 6, in top-level template code
    {% for key, value in result.iteritems() %}
jinja2.exceptions.UndefinedError: 'werkzeug.datastructures.ImmutableMultiDict object' has no attribute 'iteritems'
127.0.0.1 - - [25/Jun/2017 20:48:24] "POST /result HTTP/1.1" 500 -

您能告诉我我的代码有什么问题吗?

【问题讨论】:

  • 并非没有回溯。在您删除的帖子中,您将其包含在内,但您有一个 TemplateNotFound 异常。你还有同样的例外吗?然后通过将模板文件移动到正确的位置来修复它。
  • 我意识到自己犯了这个错误是多么愚蠢。我将 result.html 和 student.html 都移到了模板文件夹中,没有例外。取而代之的是这个。一个脚本有效,而另一个无效。
  • 我们仍然需要查看回溯。
  • 很抱歉,我该如何获得回溯?
  • 不要使用iteritems();使用items()。 Jinja 几乎是但不完全是 Python,并且您有一个未实现该方法的 ImmutableMultiDict 对象。

标签: python html flask


【解决方案1】:

您将request.form 对象传递给您的模板。正如回溯告诉您的那样,该对象没有 iteritems() 方法:

'werkzeug.datastructures.ImmutableMultiDict object' has no attribute 'iteritems'

字典上的 iteritems() 方法是 Python 2 特定的;在 Python 3 中,只需使用 items():

{% for key, value in result.items() %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2012-11-05
    • 2011-05-05
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多