render_template() takes exactly 1 argument

python新手,剛開始用Pycharm。
今天遇到一個很蠢的問題。。以爲是python版本不兼容發現是我想多了。。浪費了時間,希望給其他遇到這種智障問題的朋友提供一個提醒。。

錯誤是這樣的:

TypeError
TypeError: render_template() takes exactly 1 argument (2 given)

Traceback (most recent call last)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/sherry/PycharmProjects/c2/c2.py", line 16, in profile
return render_template("profile.html", uid )
TypeError: render_template() takes exactly 1 argument (2 given)

一直以爲是包的問題,結果並不是!
劃重點。。。

TypeError: render_template() takes exactly 1 argument (2 given)

問題&解決方案:
render_template真的只接受一個位置參數(模板名稱),但是其餘的參數(在* *上下文)作爲關鍵字參數提供。否則模板中沒有辦法參考你傳入的變量(你必須給他們明確的名稱),例如:

return render_template('index.html', f1=f1, f2=f2)

我錯在f1, f2沒賦值。寫代碼debug經驗不足,浪費了一些時間,寫個博客懲罰一下自己的粗心,也希望對遇到這個問題的新手有幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章