我一邊跑我flask.py應用程序面臨這樣的問題:builtins.NameError NameError:名字 '瓶' 沒有定義
回溯:
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/root/sitetoggle/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/root/sitetoggle/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/root/sitetoggle/lib/python3.4/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/sitetoggle/flaskr/flaskr.py", line 117, in index
return flask.Response(inner(), mimetype='text/html') # text/html is required for most browsers to show th$
NameError: name 'flask' is not define
這是我的燒瓶和HTML模板
flask.py
import os
import subprocess
import request
from flask import (Flask, request, session, g, redirect, url_for, abort, render_template, flash)
app = Flask(__name__) # create the application instance :)
app.config.from_object(__name__) # load config from this file , flaskr.py
@app.route('/siteOFF', methods=['GET', 'POST'])
def siteoff():
error = None
subprocess.call(['/root/sitetoggle/flaskr/testOFF.sh'], shell=True)
return render_template('layout.html', error=error)
@app.route('/siteON', methods=['GET', 'POST'])
def siteon():
def inner():
proc = subprocess.Popen(
['dmesg'], #call something with a lot of output so we can see it
shell=True,
stdout=subprocess.PIPE
)
for line in iter(proc.stdout.readline,''):
time.sleep(1) # Don't need this just shows the text streaming
yield line.rstrip() + '<br/>\n'
return flask.Response(inner(), mimetype='text/html') # text/html is required for most browsers to show th$
if __name__ == "__main__":
app.run()
的index.html
<!doctype html>
<title>Flaskr</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<div>
<h4>Turn site OFF or ON </h4>
<input type="button" name="lien1" value="Turn ON site"
onclick="self.location.href='http://10.10.1.8:5000/siteON'" style="background-color:#3cb371"
style="color:white; font-weight:bold"onclick>
<input type="button" name="lien2" value="Turn OFF site"
onclick="self.location.href='http://10.10.1.8:5000/siteOFF'" style="background-color:#1ab171"
style="color:white; font-weight:bold"onclick>
</div>
</div>
我使用flaskr應用程序,我自定義調用燒瓶頁面內bash腳本。除了我在flask.py中定義的兩個函數siteon()和siteoff()並嘗試在html頁面中調用它們之外,其他所有東西仍然正常工作。
需要導入燒瓶,從加進口'或flask'進口回覆燒瓶,在開始。 – bhansa
有一個名爲'flask.py'的文件是否正確?你在遮擋模塊嗎? – roganjosh
是的,我是新的瓶,所以現在就試着按照教程。 – mohbar