我从烧瓶开始,我经历了许多教程,一切正常。但我开始自己的应用程序,我只得到404错误。
我的apache虚拟服务器的配置是:
<VirtualHost domain:80>
ServerAdmin webmaster@domain
ServerName domain
ServerAlias domain *.domain
WSGIDaemonProcess test user=www-data group=www-data threads=5 home=/var/www-py/domain
WSGIScriptAlias / /var/www-py/domain/domain.wsgi
<Directory /var/www-py/domain>
WSGIProcessGroup test
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
domain.wsgi:
import sys, os
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
from domain import app as application
域/ __初始化__。PY
import os, sys
from flask import Flask
from datetime import *
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.debug=True
app.secret_key = 'mysecretkey'
db = SQLAlchemy(app)
域/视图/ index.py
# -*- coding: utf-8 -*-
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
@app.route('/')
def index():
return render_template('index.html')
这一切都很简单。问题是我尝试的所有应用程序都写在一个文件中。现在我试图分开将其分类为文件,以便更容易管理更大的项目。 请你帮帮我吧 谢谢。
答案 0 :(得分:1)
你有两个问题:
views/index.py
中,您实际上并未定义app
,因此如果您实际导入NameError
,则会产生views.index
。__init__.py
中,您永远不会导入views.index
,因此您的路线永远不会被添加到Flask.url_routes
地图。您有两种选择:
您可以按照文档中的说明进行循环导入:
# views.index
from flask import render_template
from domain import app
@app.route("/")
def index():
return render_template("index.html")
# __init__.py
# ... snip ...
db = SQLAlchemy(app)
# View imports need to be at the bottom
# to ensure that we don't run into problems
# with partially constructed dependencies
# as this is a circular import
# (__init__ imports views.index which imports __init__ which imports views.index ...)
from views import index
您可以将app
的创建提取到一个单独的文件中,并完全避免循环导入:
# NEW: infrastructure.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask("domain")
db = SQLAlchemy(app)
# views.index
from domain.infrastructure import app
# NEW: app.py
from domain.infrastructure import app
import domain.views.index
# __init__.py
# is now empty
答案 1 :(得分:0)
您需要在域/ init .py中导入views.index,并在域导入应用中添加&#34;&#34;在index.py中。否则它找不到应用程序