2012-06-24 12 views
6

Sto appena iniziando lo sviluppo web di Python e ho scelto Bottle come framework di scelta.creazione di sottoprogetti in bottiglia

Sto provando ad avere una struttura di progetto che sia modulare, nel senso che posso avere un'applicazione 'core' che ha moduli costruiti attorno ad esso, dove questi moduli possono essere abilitati/disabilitati durante l'installazione (o al volo, se possibile ... non sono sicuro di come lo sistemerei).

La mia classe 'principale' è la seguente:

from bottle import Bottle, route, run 
from bottle import error 
from bottle import jinja2_view as view 

from core import core 

app = Bottle() 
app.mount('/demo', core) 

#@app.route('/') 
@route('/hello/<name>') 
@view('hello_template') 
def greet(name='Stranger'): 
    return dict(name=name) 

@error(404) 
def error404(error): 
    return 'Nothing here, sorry' 

run(app, host='localhost', port=5000) 

mio 'sottoprogetto' (cioè modulo) è questo:

from bottle import Bottle, route, run 
from bottle import error 
from bottle import jinja2_view as view 

app = Bottle() 

@app.route('/demo') 
@view('demographic') 
def greet(name='None', yob='None'): 
    return dict(name=name, yob=yob) 

@error(404) 
def error404(error): 
    return 'Nothing here, sorry' 

Quando vado a http://localhost:5000/demo nel browser, esso mostra un 500 errore. L'uscita dal server bottiglia è:

localhost - - [24/Jun/2012 15:51:27] "GET/HTTP/1.1" 404 720 
localhost - - [24/Jun/2012 15:51:27] "GET /favicon.ico HTTP/1.1" 404 742 
localhost - - [24/Jun/2012 15:51:27] "GET /favicon.ico HTTP/1.1" 404 742 
Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.9-py2.7.egg/bottle.py", line 737, in _handle 
    return route.call(**args) 
    File "/usr/local/lib/python2.7/dist-packages/bottle-0.10.9-py2.7.egg/bottle.py", line 582, in mountpoint 
    rs.body = itertools.chain(rs.body, app(request.environ, start_response)) 
TypeError: 'module' object is not callable 

La struttura delle cartelle è:

index.py 
views (folder) 
|-->hello_template.tpl 
core (folder) 
|-->core.py 
|-->__init__.py 
|-->views (folder) 
|--|-->demographic.tpl 

Non ho idea di quello che sto facendo (sbagliato) :)

Qualcuno ha qualche idea di come questo può/dovrebbe essere fatto?

Grazie!

risposta

8

Si sta passando il modulo "core" alla funzione mount(). Invece devi passare l'oggetto app bottiglia alla funzione mount(), quindi la chiamata sarebbe come questa.

app.mount("/demo",core.app) 

Ecco i documenti formali per la funzione mount().

mount(prefix, app, **options)[source] 

Monte un'applicazione (bottiglia o WSGI normale) a un URL specifico prefisso.
Esempio:

root_app.mount('/admin/', admin_app) 

Parametri:
prefix - percorso prefisso o mount-point. Se termina con una barra , quella barra è obbligatoria.
app - un'istanza di bottiglia o di un'applicazione WSGI

+0

ahhh ok, Gotcha. funziona ora :) Grazie Rohan! – Jarrett

Problemi correlati