Pyramid 使用总结1

Here's something I made to help you understand Pyramid well.

You may need to read very specific tutorials in details in those 5 steps:

1. A Hello World! using Pyramid web framework http://docs.pylonsproject.org/projects/pyramid/en/latest/

My knowledge and experience:

Raju's blog is really helpful to help people understand the Pyramid web framework very well.

2.youtube视频http://www.youtube.com/watch?v=L2KSsqwwH9M&list=PL6-GrNvaJuAhcBuzrL6UR-LhDbNkNhFwv

3.Raju的pyramid 入门博客:http://fosshelp.blogspot.in 也可以直接看 http://fosshelp.blogspot.in/search/label/Pyramid 需要翻强。goAgent或者VPN都可以,这个如果连墙都越不了的话,就别看博客了。

4.how to create a pyramid apphttp://docs.pylonsproject.org/projects/pyramid/en/latest/narr/project.html

Many people on irc.freenode.com suggested me to follow that tutorial :http://docs.pylonsproject.org/projects/pyramid/en/master/tutorials/wiki2/index.html

But I have read through 

5.SQLAlchemy + URL Dispatch Wiki Tutorial (Make a Wiki site!)

http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/index.html

already. Anyway those two tutorials of different versions are almost the same. 

We are good to go.

Key Note:

1. undertanding the three files that you created by 

pcreate -s alchemy my_alchemy

my_alchemy is the name of your project

2. install your app

../bin/python setup.py develop

Or you could use 

../bin/python setup.py test -q

to check out the ingredients of your project

Or you may need to use nose and 

Basic Layouts! Pyramid 应用结构

http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki2/basiclayout.html

-------1. __init__.py    (To configure Database, Server, Route, Views )

I didn't change it. Keep it by default. 

Of course, if you need some functionalities, just do it.

-------2. views.py   (   To declare a lot of view callables, note the decorator @view_config(route='/', renderer='templates/template.pt')      this decorator make the request easily be mapped to the route   ) 

Note that:  To get time, we need to import time first. And only importing two class from time will not work as the codes committed above.

I added another element of the dictionary above. 

the @view_config decorator is really nice. it is like a bridge between route and Response

Have a look at the template.pt which I changed:

I added ${localtime} and some other fancy words around the ${localtime}

Everytime we request the localhost:8080/ , we will get the page which is pre-generated by python interpreter via the special system of views.py , __init__.py, models. py . 

Let's see what I got

there are 2 "Change Me in views.py"

it's because that in the views i did

return {'one':one, 'project':'Change Me in views.py'*2, 'localtime':just_time}

the string is multiplied by 2.

-------3. models.py  ( To create objects of the those models which are mainly oriented to Databases, like PostgreSQL, MySQL, Sqlite, etc.. Have to say that SQLAlchemy is so great!!! )

 

SQLalchemy.orm

the class Page is like to initialize

                    what elements the table should have.

                    1. __tablename__ = "pages"

                    2. elements such as : name, data, height or etc..   NEED Column to assign!!!

                         That's the reason why we do this:

from sqlalchemy import (
    Column,
    Integer,
    Text,
    #String,         # if you need to store in string !!!! And other data types
)

Do as Romans Do

Then Change the ./script/initializedb.py   ( in your project's folder)

This will add some values to the table pages as above

Note: import first !

then go to do:

./bin/initialize_tutorial_db development.ini

you will see:

2011-11-27 01:22:45,277 INFO  [sqlalchemy.engine.base.Engine][MainThread]
                              PRAGMA table_info("pages")
2011-11-27 01:22:45,277 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2011-11-27 01:22:45,277 INFO  [sqlalchemy.engine.base.Engine][MainThread]
CREATE TABLE pages (
      id INTEGER NOT NULL,
      name TEXT,
      data TEXT,
      PRIMARY KEY (id),
      UNIQUE (name)
)


2011-11-27 01:22:45,278 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2011-11-27 01:22:45,397 INFO  [sqlalchemy.engine.base.Engine][MainThread]
                              COMMIT
2011-11-27 01:22:45,400 INFO  [sqlalchemy.engine.base.Engine][MainThread]
                              BEGIN (implicit)
2011-11-27 01:22:45,401 INFO  [sqlalchemy.engine.base.Engine][MainThread]
                              INSERT INTO pages (name, data) VALUES (?, ?)
2011-11-27 01:22:45,401 INFO  [sqlalchemy.engine.base.Engine][MainThread]
                              ('FrontPage', 'This is the front page')
2011-11-27 01:22:45,402 INFO  [sqlalchemy.engine.base.Engine][MainThread]
                              COMMIT

for more information please visit : http://docs.pylonsproject.org/projects/pyramid/en/master/tutorials/wiki2/definingmodels.html

 

Now there is another table which is storing in the db file tutorial.sqlite

Here's how to check out the sqlite data:

.help

will give you a lot help of how to use sqlite

( But you need to install sqlite first on ubuntu and debian,   

sudo apt-get install libsqlite3-dev

or on archlinux,

sudo pacman -S sqlite

)

原文地址:https://www.cnblogs.com/spaceship9/p/3019284.html