2

我正在使用 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>

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()

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) 

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!')

template.tpl

<html>
<head>

<link rel="stylesheet" type="text/css" href="static/prettify.css" />
</head>
<body>
{{text}}
</body>

</html>

目录列表

temp/
  adapter.wsgi
  index.py
  routes.py
  static/
     prettify.css
  views/
     template.tpl

我的问题是,每当我尝试使用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/相同?

任何帮助都会非常有帮助

4

3 回答 3

15

问题

有问题的行是这一行:

<link rel="stylesheet" type="text/css" href="static/prettify.css" />

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.csstemp被认为是根目录中的一个文件,而不是它自己的子目录。

解决方案

没有可行的方法来使用单个相对地址来引用静态资源。您的应用程序可能会有“嵌套”路径,如/article/some-name、 或/view/content/566或类似的东西,以及像/.

您可以尝试/temp/static/prettify.css在模板中指定基于根的路径,例如 ,但这意味着如果您重新定位应用程序本身(例如 to myapp.example.com/from example.com/myapp/),则必须更改模板。

相反,您需要告诉框架为您需要使用的资源创建正确的路径。Bottle 有一个名为get_url的函数来实现这一点。不幸的是,瓶子教程中没有提到它。

编码

这是你应该做的。

template.tpl,调用get_url引用静态处理程序:

<link rel="stylesheet" type="text/css" 
      href="{{ get_url('static', filename='prettify.css') }}" />

routes.py,导入get_url

from bottle import Bottle, run, route, static_file, view, template, 
                   post, request, get_url

然后,命名您的处理程序,以便您可以将其名称传递给get_url

@app.route('/static/<filename>', name='static')
def server_static(filename):
    return static_file(filename, root='static')

最后,get_url在渲染模板时提供实际值作为模板参数:

@app.route('/') 
def index(): 
    return template('template', text='This is index page!', get_url=get_url)

或者,不要get_url在每个处理程序中提供,而是在以下位置设置模板默认值index.py

from Bottle import SimpleTemplate
SimpleTemplate.defaults["get_url"] = app.get_url

警告:最后一种方法似乎没有记录,但瓶子的作者在邮件列表中进行了解释

最后的想法

由于网站上的每个页面都应该有一个规范地址,因此您可能希望选择一种形式(带有斜杠或不带斜杠)作为规范,并从另一种形式添加某种重定向。

于 2012-03-01T23:01:53.623 回答
1

另一种解决方法是在您的 Apache 配置文件中添加此重定向:

RedirectMatch 301 ^/(temp)$ /$1/

这将在索引页的末尾添加 /,因此您不必修改代码。

于 2014-10-14T10:09:33.343 回答
0

一种解决方法是添加:

<base href="/temp/">

到模板中的头部。

于 2014-03-04T21:57:48.297 回答