Anu*_*nuj 2 python apache bottle
我正在使用Bottle通过apache提供测试文件.
以下是我的apache配置:
WSGIDaemonProcess temp user=www-data group=www-data processes=1 threads=5
WSGIScriptAlias /temp /opt/gridops/usage/temp/adapter.wsgi
<Directory /opt/gridops/usage/temp>
WSGIProcessGroup temp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
Run Code Online (Sandbox Code Playgroud)
adapter.wsgi:
import os,sys
os.chdir(os.path.dirname(__file__))
sys.path = ['/opt/gridops/usage/temp'] + sys.path
os.chdir(os.path.dirname(__file__))
sys.stdout = sys.stderr
import bottle
print "++"*10
import index # This loads your application
application = bottle.default_app()
Run Code Online (Sandbox Code Playgroud)
index.py:
from bottle import mount, run
from routes import app
from bottle import default_app
default_app.push(app)
#run()
#run(app=app, host='192.168.1.3', port=8085)
Run Code Online (Sandbox Code Playgroud)
routes.py:
from bottle import Bottle , run,route,static_file,view,template,post,request
app = Bottle()
print str(dir(app))
@app.route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='static')
@app.route('/')
def index():
return template('template',text='This is index page!')
Run Code Online (Sandbox Code Playgroud)
template.tpl:
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/prettify.css" />
</head>
<body>
{{text}}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
目录列表
temp/
adapter.wsgi
index.py
routes.py
static/
prettify.css
views/
template.tpl
Run Code Online (Sandbox Code Playgroud)
我的问题是每当我尝试使用http://192.168.1.3/temp显示没有静态文件的网页访问Bottle应用程序时,但每当我访问http://192.168.1.3/temp/[请注意额外/]页面正确加载.我应该做的修改方案,使得两者的结果
http://192.168.1.3/temp,并http://192.168.1.3/temp/成为一样的吗?
任何帮助都会非常有帮助
Hel*_*lgi 14
问题是这一行:
<link rel="stylesheet" type="text/css" href="static/prettify.css" />
Run Code Online (Sandbox Code Playgroud)
CSS文件的地址是相对的,因此从加载的页面位置计算完整的绝对地址.
因为http://192.168.1.3/temp/,它将是http://192.168.1.3/temp/static/prettify.css(正确的).
因为http://192.168.1.3/temp,它会http://192.168.1.3/static/prettify.css.temp被认为是根目录中的文件,而不是其自身的子目录.
没有可行的方法来使用单个相对地址来引用静态资源.您的应用程序可能具有"嵌套"路径,如/article/some-name或/view/content/566,或类似的东西,以及简单的路径/.
您可以尝试/temp/static/prettify.css在模板中指定基于根的路径,但这意味着如果您重新定位应用程序本身(例如,myapp.example.com/从中 example.com/myapp/),则必须更改模板.
相反,您需要告诉框架为您需要使用的资源建立正确的路径.Bottle有一个名为get_url的函数来促进这一点.不幸的是,瓶子教程中没有提到它.
这是你应该做的.
在template.tpl,调用get_url引用静态处理程序:
<link rel="stylesheet" type="text/css"
href="{{ get_url('static', filename='prettify.css') }}" />
Run Code Online (Sandbox Code Playgroud)
在routes.py,导入get_url:
from bottle import Bottle, run, route, static_file, view, template,
post, request, get_url
Run Code Online (Sandbox Code Playgroud)
然后,为您的处理程序命名,以便将其名称传递给get_url:
@app.route('/static/<filename>', name='static')
def server_static(filename):
return static_file(filename, root='static')
Run Code Online (Sandbox Code Playgroud)
最后,get_url在渲染模板时提供实际作为模板参数:
@app.route('/')
def index():
return template('template', text='This is index page!', get_url=get_url)
Run Code Online (Sandbox Code Playgroud)
或者,不是get_url在每个处理程序中提供,而是在以下位置设置模板默认值index.py:
from Bottle import SimpleTemplate
SimpleTemplate.defaults["get_url"] = app.get_url
Run Code Online (Sandbox Code Playgroud)
警告:最后一种方法似乎没有记录,但是由邮件列表上的Bottle的作者解释.
由于网站上的每个页面都应该具有规范地址,因此您可能希望选择一个表单(带有斜杠或没有斜杠)作为规范,并从另一个表单添加某种重定向.
| 归档时间: |
|
| 查看次数: |
8956 次 |
| 最近记录: |