-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
但是我們仍然需要看到回溯。 –