Python 省略尾部斜杠时未加载到瓶子应用程序中的静态文件
我正在使用瓶子通过apache提供一个测试文件 以下是我的apache配置:Python 省略尾部斜杠时未加载到瓶子应用程序中的静态文件,python,apache,bottle,Python,Apache,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
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>
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)
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!')
from Bottle import SimpleTemplate
SimpleTemplate.defaults["get_url"] = app.get_url
routes.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)
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!')
from Bottle import SimpleTemplate
SimpleTemplate.defaults["get_url"] = app.get_url
目录列表
temp/
adapter.wsgi
index.py
routes.py
static/
prettify.css
views/
template.tpl
我的问题是每当我尝试使用http://192.168.1.3/temp
网页显示时没有静态文件,但只要我访问http://192.168.1.3/temp/
[请注意额外的/
]页面加载正确。我应该做什么修改以使两者的结果一致
http://192.168.1.3/temp
和http://192.168.1.3/temp/
是否变得相同
任何帮助都会对解决问题大有裨益
有问题的一行是:
<link rel="stylesheet" type="text/css" href="static/prettify.css" />
在routes.py
中,导入get\u url
:
from bottle import Bottle, run, route, static_file, view, template,
post, request, get_url
@app.route('/static/<filename>', name='static')
def server_static(filename):
return static_file(filename, root='static')
然后,命名处理程序,以便将其名称传递给get\u url
:
from bottle import Bottle, run, route, static_file, view, template,
post, request, get_url
@app.route('/static/<filename>', name='static')
def server_static(filename):
return static_file(filename, root='static')
或者,不是在每个处理程序中提供get\u url
,而是在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)
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!')
from Bottle import SimpleTemplate
SimpleTemplate.defaults["get_url"] = app.get_url
警告:最后一种方法似乎没有记录在案,但被取消了
最后的想法
由于网站上的每个页面都应该有一个规范地址,因此您可能希望选择一个表单(带尾随斜杠或不带尾随斜杠)作为规范,并从另一个表单添加某种重定向。一种解决方法是添加:
<base href="/temp/">
另一个解决方法是在Apache配置文件中添加此重定向:
RedirectMatch 301 ^/(temp)$ /$1/
这将在索引页的末尾添加一个/,这样您就不必修改代码。我已经按照这里的步骤进行了操作,在处理样式表href属性时,我得到一个“NameError:name'get_url'未定义”。我会错过什么?谢谢。@Shawn:您应该将其作为模板参数提供(
get\u url=get\u url
调用template
),或者设置模板默认值(SimpleTemplate.defaults[“get\u url”]=app.get\u url
)。如果您这样做了,错误仍然存在,我建议您使用代码的摘录创建一个新问题,并在此处发布一个链接。谢谢。我两次都做了,但仍然出错,只是尝试了其他方法,编写了get\u url=app.get\u url。这是可行的,或者至少错误已经消失,应用程序将实际加载。现在我必须弄清楚为什么我的css没有被加载。谢谢。我刚才在会议上问了一个问题。感谢您提供的任何帮助。@G.A.:get\u url
仍然是battle
类的一种方法,无论如何,您最好明确创建一个应用程序。另见