0
我有下面的代碼作爲一個掛架應用中間件:塔自定義中間件返回404
import testing.model as model
import re
from pylons.controllers.util import abort
class SubdomainCheckMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
if 'subdomaincheck' in environ:
return self.app(environ, start_response)
p = re.compile('([a-z0-9\-]+)', re.IGNORECASE)
subdomain = p.match(environ['HTTP_HOST']).group(0)
query = "SELECT \"nspname\" FROM \"pg_namespace\" WHERE \"nspname\" = '%s';" % subdomain
result = model.meta.Session.execute(query)
for row in result:
if row['nspname'] == subdomain:
environ['subdomaincheck'] = 'done'
return self.app(environ, start_response)
它所basicly不會被檢查,如果在PostgreSQL的架構存在與給定的子域,但我需要它如果模式不存在則返回404,我該怎麼做?如果你想要的東西更復雜,你可以使用一些其他中間件來處理錯誤給你
if error:
start_response("404 Not Found", [('Content-type', 'text/plain')])
return ['Page not found']
:
完美的作品,謝謝! – 2010-01-19 11:04:56