我刚开始学习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.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 -
您能告诉我的代码有什么问题吗?
答案 0 :(得分:0)
您将request.form
对象传递给了模板。跟踪告诉您,该对象没有iteritems()
方法:
'werkzeug.datastructures.ImmutableMultiDict object' has no attribute 'iteritems'
字典上的iteritems()
方法是Python 2特有的;在Python 3中,只需使用items()
:
{% for key, value in result.items() %}