Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

37.6. DEFINING VIEWS

37.6.4 Adding Templates

The views we’ve added all reference a template. Each template is a Chameleon ZPT template. These templates will live in the templates directory of our tutorial package.

The view.pt Template

The view.pt template is used for viewing a single wiki page. It is used by the view_page view function. It should have a div that is “structure replaced” with the content value provided by the view. It should also have a link on the rendered page that points at the “edit” URL (the URL which invokes the edit_page view for the page being viewed).

Once we’re done with the view.pt template, it will look a lot like the below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">

<head>

<title>${page.name} - Pyramid tutorial wiki (based on TurboGears 20-Minute Wiki)</title>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <meta name="keywords" content="python web application" />

<meta name="description" content="pyramid web application" /> <link rel="shortcut icon"

href="${request.static_url(’tutorial:static/favicon.ico’)}" /> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/pylons.css’)}" type="text/css" media="screen" charset="utf-8" />

<!--[if lte IE 6]> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/ie6.css’)}" type="text/css" media="screen" charset="utf-8" />

<![endif]-->

</head>

<body>

<div id="wrap">

<div id="top-small">

<div class="top-small align-center">

<div>

<img width="220" height="50" alt="pyramid" src="${request.static_url(’tutorial:static/pyramid-small.png’)}" />

</div>

467

37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

</div>

</div>

<div id="middle">

<div class="middle align-right">

<div id="left" class="app-welcome align-left"> Viewing <b><span tal:replace="page.name">Page Name

Goes Here</span></b><br/>

You can return to the

<a href="${request.application_url}">FrontPage</a>.<br/>

</div>

<div id="right" class="app-welcome align-right"></div>

</div>

</div>

<div id="bottom">

<div class="bottom">

<div tal:replace="structure content"> Page text goes here.

</div>

<p>

<a tal:attributes="href edit_url" href=""> Edit this page

</a>

</p>

</div>

</div>

</div>

<div id="footer"> <div class="footer"

>© Copyright 2008-2011, Agendaless Consulting.</div>

</div>

</body>

</html>

latex-note.png

The names available for our use in a template are always those that are present in the dictionary returned by the view callable. But our templates make use of a request object that none of our tutorial views return in their dictionary. This value appears as if “by magic”. However, request is one of several names that are available “by default” in a template when a template renderer is used. See *.pt or *.txt: Chameleon Template Renderers for more information about other names that are available by default in a template when a Chameleon template is used as a renderer.

468

37.6. DEFINING VIEWS

The edit.pt Template

The edit.pt template is used for adding and editing a wiki page. It is used by the add_page and edit_page view functions. It should display a page containing a form that POSTs back to the “save_url” argument supplied by the view. The form should have a “body” textarea field (the page data), and a submit button that has the name “form.submitted”. The textarea in the form should be filled with any existing page data when it is rendered.

Once we’re done with the edit.pt template, it will look a lot like the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:tal="http://xml.zope.org/namespaces/tal">

<head>

<title>${page.name} - Pyramid tutorial wiki (based on TurboGears 20-Minute Wiki)</title>

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> <meta name="keywords" content="python web application" />

<meta name="description" content="pyramid web application" /> <link rel="shortcut icon"

href="${request.static_url(’tutorial:static/favicon.ico’)}" /> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/pylons.css’)}" type="text/css" media="screen" charset="utf-8" />

<!--[if lte IE 6]> <link rel="stylesheet"

href="${request.static_url(’tutorial:static/ie6.css’)}" type="text/css" media="screen" charset="utf-8" />

<![endif]-->

</head>

<body>

<div id="wrap">

<div id="top-small">

<div class="top-small align-center">

<div>

<img width="220" height="50" alt="pyramid" src="${request.static_url(’tutorial:static/pyramid-small.png’)}" />

</div>

</div>

</div>

<div id="middle">

<div class="middle align-right">

<div id="left" class="app-welcome align-left">

Editing <b><span tal:replace="page.name">Page Name Goes Here</span></b><br/>

469

37. SQLALCHEMY + URL DISPATCH WIKI TUTORIAL

You can return to the

<a href="${request.application_url}">FrontPage</a>.<br/>

</div>

<div id="right" class="app-welcome align-right"></div>

</div>

</div>

<div id="bottom">

<div class="bottom">

<form action="${save_url}" method="post">

<textarea name="body" tal:content="page.data" rows="10" cols="60"/><br/>

<input type="submit" name="form.submitted" value="Save"/>

</form>

</div>

</div>

</div>

<div id="footer"> <div class="footer"

>© Copyright 2008-2011, Agendaless Consulting.</div>

</div>

</body>

</html>

Static Assets

Our templates name a single static asset named pylons.css. We don’t need to create this file within our package’s static directory because it was provided at the time we created the project. This file is a little too long to replicate within the body of this guide, however it is available online.

This

CSS file will be accessed via e.g. http://localhost:6543/static/pylons.css

by

virtue

of the call to add_static_view directive we’ve

made in the __init__.py

file.

 

Any

number and type

of

static

assets

can

be

placed in

this directory (or subdirecto-

ries)

and are just referred to

by

URL

or by

using

the

convenience method static_url e.g.

request.static_url(’{{package}}:static/foo.css’) within templates.

37.6.5 Adding Routes to __init__.py

The __init__.py file contains pyramid.config.Configurator.add_route() calls which serve to add routes to our application. First, we’ll get rid of the existing route created by the template using the name ’home’. It’s only an example and isn’t relevant to our application.

We then need to add four calls to add_route. Note that the ordering of these declarations is very important. route declarations are matched in the order they’re found in the __init__.py file.

470

37.6. DEFINING VIEWS

1.Add a declaration which maps the pattern / (signifying the root URL) to the route named view_wiki. It maps to our view_wiki view callable by virtue of the @view_config attached to the view_wiki view function indicating route_name=’view_wiki’.

2.Add a declaration which maps the pattern /{pagename} to the route named view_page.

This is the regular view for a page. It maps to our view_page view callable by virtue of the @view_config attached to the view_page view function indicating route_name=’view_page’.

3.Add a declaration which maps the pattern /add_page/{pagename} to the route named add_page. This is the add view for a new page. It maps to our add_page view callable by virtue of the @view_config attached to the add_page view function indicating route_name=’add_page’.

4.Add a declaration which maps the pattern /{pagename}/edit_page to the route named edit_page. This is the edit view for a page. It maps to our edit_page view callable by virtue of the @view_config attached to the edit_page view function indicating route_name=’edit_page’.

As a result of our edits, the __init__.py file should look something like:

1

from pyramid.config

import Configurator

2

from

sqlalchemy import engine_from_config

3

 

 

 

4

from

.models import

DBSession

5

6 def main(global_config, **settings):

7""" This function returns a Pyramid WSGI application.

8"""

9engine = engine_from_config(settings, ’sqlalchemy.’)

10DBSession.configure(bind=engine)

11config = Configurator(settings=settings)

12config.add_static_view(’static’, ’static’, cache_max_age=3600)

13config.add_route(’view_wiki’, ’/’)

14config.add_route(’view_page’, ’/{pagename}’)

15config.add_route(’add_page’, ’/add_page/{pagename}’)

16config.add_route(’edit_page’, ’/{pagename}/edit_page’)

17config.scan()

18return config.make_wsgi_app()

(The highlighted lines are the ones that need to be added or edited.)

471

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]