-2
我刚开始学习Flask并引用一个教程来运行一小段测试代码。在Flask中显示输出时出现内部服务器错误500
下面是代码:
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.html
和result.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,并且没有出现异常。相反,这。一个脚本工作,而另一个脚本不工作。 – Kshitiz
但是我们仍然需要看到回溯。 –