项目结构
├── app
│ ├── forms
│ │ ├──
__init__.py│ │ ├──
login.py │ ├──
__init__.py│ └── views
│ ├──
admin.py│ ├──
blog.py│ ├──
book.py│ ├──
__init__.py├── config
│ ├──
default.py│ ├──
development.py│ ├──
__init__.py##
run.py 内容
```
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run()
```
## app/
__int__.py```
def create_app():
app = Flask(__name__)
config = load_config()
app.config.from_object(config)
register_routes(app)
return app
def register_routes(app):
from .views import admin, blog, book
app.register_blueprint(
admin.site, url_prefix='')
app.register_blueprint(
blog.site, url_prefix='/blog')
app.register_blueprint(
book.site, url_prefix='/book')
```
## views/
blog.py```
from flask_flatpages import FlatPages
from .. import create_app
site = Blueprint('blog',__name__)
app = create_app()
flatpages = FlatPages(app)
```
运行时出错
```
Traceback (most recent call last):
File "
run.py", line 13, in <module>
app = create_app()
File "/home/honmaple/website/app/
__init__.py", line 21, in create_app
register_routes(app)
File "/home/honmaple/website/app/
__init__.py", line 29, in register_routes
from .views import admin, blog, book
File "/home/honmaple/website/app/views/
blog.py", line 18, in <module>
app = create_app()
File "/home/honmaple/website/app/
__init__.py", line 21, in create_app
register_routes(app)
File "/home/honmaple/website/app/
__init__.py", line 29, in register_routes
from .views import admin, blog, book
ImportError: cannot import name 'blog'
```
### views/
admin.py book.py 与
blog.py 不同的地方就是
```from .. import create_app```
还有我本机是 python3.5,服务器上是 python3.4,本机测试正常,服务器上运行出错,难道是版本的原因
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
https://www.v2ex.com/t/238187
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.