URL派发如何使用金字塔服务于提供静态图像文件?

问题描述

我从快速教程页面here创建了一个简单的Pyramid应用程序,其中包含与问题相关的以下文件:

  1. private void DgvGrd_SizeChanged(object sender,EventArgs e) { dgvGrd.Columns[0].Width = (int)(dgvGrd.Width * 0.2); dgvGrd.Columns[1].Width = (int)(dgvGrd.Width * 0.2); dgvGrd.Columns[2].Width = (int)(dgvGrd.Width * 0.4); dgvGrd.Columns[3].Width = (int)(dgvGrd.Width * 0.2); // also may be a good idea to set FILL for the last column // to accomodate the round up in conversions dgvGrd.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; }
tutorial/__init__.py
  1. from pyramid.config import Configurator def main(global_config,**settings): config = Configurator(settings=settings) config.include('pyramid_chameleon') config.add_route('home','/') config.add_route('hello','/howdy') config.add_static_view(name='static',path='tutorial:static') config.add_route('image','/{filename}') config.scan('.views') return config.make_wsgi_app()
tutorial/views/views.py
  1. from pyramid.view import ( view_config,view_defaults ) @view_defaults(renderer='../templates/home.pt') class TutorialViews: def __init__(self,request): self.request = request @view_config(route_name='home') def home(self): return {'name': 'Home View'} @view_config(route_name='hello') def hello(self): return {'name': 'Hello View'} @view_config(route_name='image',renderer='../templates/image.pt') def image(request): filename = request.matchdict.get('filename') return {'name': 'Hello View','filename': filename}
tutorial/templates/image.pt

我已将图像文件放置在路径<!DOCTYPE html> <html lang="en"> <head> <title>Quick Tutorial: ${name}</title> <link rel="stylesheet" href="${request.static_url('tutorial:static/app.css') }"/> </head> <body> <h1>Hi ${name}</h1> <img src="../static/images/${filename}"> </body> </html> 上。现在,这是我尝试过的3种情况以及使用tutorial/static/images/test.jpeg启动服务器时的结果:

  1. pserve development.ini --reload中的路由配置:tutorial/__init__.py。当我访问config.add_route('image','/{filename}')时,可以看到该图像,并且一切正常。
  2. localhost:6543/test.jpeg中的路由配置:tutorial/__init__.py。当我访问config.add_route('image','/foo/{filename}')时,可以看到该图像,并且一切正常。
  3. localhost:6543/foo/test.jpeg中的路由配置:tutorial/__init__.py。当我访问config.add_route('image','/foo/bar/{filename}')时,这是我 看到图像的时候。

在上述情况3)中,我尝试了一些操作,并且只有在文件localhost:6543/foo/bar/test.jpeg中将行tutorial/templates/image.pt更改为<img src="../static/images/${filename}">时,才能看到图像。我似乎无法理解为什么Pyramid强迫我在模板中添加另一个目录层以查看图像。谁能解释为什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)