2

我从烧瓶开始,我经历了许多教程并且一切正常。但是我开始了自己的应用程序,但我只得到错误 404 not found。

我的 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>

域.wsgi:

import sys, os

current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(current_dir)
from domain import app as application

域/__init__.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')

这就是所有和简单的应用程序。问题是我尝试过的所有应用程序都写在一个文件中。现在我正在尝试将其分离以将其分类为文件,以便于管理更大的项目。请你帮帮我。谢谢你。

4

2 回答 2

2

你有两个问题:

  1. 因为views/index.py你实际上并没有定义,所以如果你真的 import app,这将导致一个。NameErrorviews.index
  2. __init__.py您从不导入views.index的情况下,您的路线永远不会被添加到Flask.url_routes地图中。

你有两个选择:

  1. 您可以按照文档中的说明采用循环导入方式:

    # 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
    
  2. 您可以将创建拉到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
    
于 2013-05-27T20:55:38.977 回答
1

您需要在 domain/ init .py 中导入 views.index,并在 index.py 中添加“from domain import app”。否则找不到应用

于 2013-06-02T15:35:25.303 回答