How can I write a callable view that displays static html files in a pyramid?

In my pyramid app, I have some static html files under tutorial / tutorial / pages / name.html (for example). How can I write a view called to do this? Will this work?

     @view_config(renderer='view_page')
     def view_page(request):
         return {} # no values have to be passed to the template

      

then in init .py file

config.add_route('view_page', 'tutorial:pages/{name}.html')

      

What do I need to enable the def view_page (request) function to call this name.html file and then display its contents?

+3


source to share


1 answer


Pyramid static_view

is a view capable of serving files from a directory. The part you didn't really explain is what the URLs are for these static pages. For example, if they are all under a common prefix, you can use static_view

(option 1). If this is not the case, then you need to create a view on the page and serve it directly (option 2).

option 1

Url:

/foo/bar.html
/foo/baz/boo.html

      

static view:

config.add_static_view('/foo', 'tutorial:pages')

      



tutorial / page hierarchy:

tutorial/pages/bar.html
tutorial/pages/baz/boo.html

      

add_static_view

effective as a challenge add_route('foo', '/foo/*subpath')

and it serves subpath

relatively tutorial:pages

.

option 2

config.add_route('foo', '/foo')
config.add_route('bar', '/foo/bar')

@view_config(route_name='foo', renderer='tutorial:pages/foo.html.mako')
@view_config(route_name='bar', renderer='tutorial:pages/bar.html.mako')
def static_view(request):
    return {}

      

Note the suffix .mako

to invoke the mako renderer. There is no renderer by default .html

, but you can do one.

+4


source







All Articles