我从烧瓶开始,我的app.py
上有这段代码:
from flask import Flask, render_template, request
from flask.ext.uploads import UploadSet, configure_uploads, IMAGES
app = Flask(__name__)
photos = UploadSet('photos', IMAGES)
app.config['UPLOADED_PHOTOS_DEST'] = 'static/img'
configure_uploads(app, photos)
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST' and 'photo' in request.files:
filename = photos.save(request.files['photo'])
return filename
return render_template('upload.html')
if __name__ == '__main__':
app.run(debug=True)
我的upload.html
代码:
<html>
<head>
<title>Upload</title>
</head>
<body>
<form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}">
<input type=file name=photo>
<input type="submit">
</form>
</body>
</html>
这是文件层次结构:
├── image_upload
│ ├── app
│ │ ├── app.py
│ │ ├── app.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── routes.py
│ │ ├── routes.pyc
│ │ └── upload.html
│ ├── image_upload.py
│ ├── image_upload.pyc
│ └── static
│ ├── img
│ └── templates
│ └── upload.html
└── install.sh
我的__init__.py
文件:
from flask import Flask
app = Flask(__name__)
我的routes.py
虽然我没有使用它:
from app import app
@app.route('/')
@app.route('/upload')
def index():
return upload
image_upload.py
文件:
from app import app
当我浏览它时总是抛出错误404,它可能是routes.py
文件吗?
我对flask
更新鲜,所以任何帮助都会受到高度赞赏。
如果我从 init 导入路径文件,它会抛出我:
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/paper_flask/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 513, in main
cli.main(args=args, prog_name=name)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 380, in main
return AppGroup.main(self, *args, **kwargs)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 423, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 152, in __init__
self._load_unlocked()
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 176, in _load_unlocked
self._app = rv = self.loader()
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 237, in load_app
rv = locate_app(self.app_import_path)
File "/home/kristian/.virtualenvs/paper_flask/local/lib/python2.7/site-packages/flask/cli.py", line 90, in locate_app
__import__(module)
File "/home/kristian/paper/remote_sensing_1/image_upload/image_upload.py", line 1, in <module>
from app import app
File "/home/kristian/paper/remote_sensing_1/image_upload/app/__init__.py", line 5, in <module>
from app import routes
ImportError: cannot import name routes