turbogears
−Table of Contents
Turbo Gears
tg-admin quickstart jmeno projektu: foto
SQLite
sqlite3 devdata.sqlite
CREATE TABLE fotografie ( id INTEGER PRIMARY KEY, nazev TEXT, soubor TEXT );
INSERT INTO "fotografie" VALUES(1,'Acko','aaa.txt');
Některé příkazy sqlite3
.help .databases .tables .schema fotografie .dump fotografie .explain on select * from fotografie; .mode list .mode html .mode insert .headers on .explain on .quit
První verze, adresář foto/foto
Soubor model.py
from datetime import datetime from turbogears.database import PackageHub from sqlobject import * from turbogears import identity hub = PackageHub("foto") __connection__ = hub class Fotografie(SQLObject): nazev = StringCol() soubor = StringCol()
Soubor controllers.py
from turbogears import controllers, expose, flash # from foto import model import pkg_resources try: pkg_resources.require("SQLObject>=0.8,<=0.10.0") except pkg_resources.DistributionNotFound: import sys print >> sys.stderr, """You are required to install SQLObject but appear not to have done so. Please run your projects setup.py or run `easy_install SQLObject`. """ sys.exit(1) import logging log = logging.getLogger("foto.controllers") class Root(controllers.RootController): @expose(template="foto.templates.welcome") def index(self): import time # log.debug("Happy TurboGears Controller Responding For Duty") flash("Your application is now running") return dict(now=time.ctime()) @expose(template="foto.templates.pokus") def pokus(self): import time log.debug("*** Nekdo navstivil fologalerii ***") flash("Fotografie") return dict(current_time=time.ctime())
Soubor template/pokus.kid
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="'master.kid'"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/> <title>Fotografie</title> </head> <body> <div id="sidebar"> <h2>Plovoucí text </h2> Nejaky text <br/> Odkaz na <a href="http://www.turbogears.org">TurboGears website</a> </div> <div class="notice"> <span py:replace="current_time"> tento text bude nahrazen hodnou promenne current_time</span> </div> </body> </html>
Druhá verze
Data
sqlite3 devdata.sqlite
CREATE TABLE fotografie ( id INTEGER PRIMARY KEY, nazev TEXT, soubor TEXT ); INSERT INTO "fotografie" VALUES(1,'Kvetouci strom','0053.jpg'); INSERT INTO "fotografie" VALUES(2,'Hlemyzd','4635.jpg'); INSERT INTO "fotografie" VALUES(3,'Kobylka','1940.jpg'); INSERT INTO "fotografie" VALUES(4,'Safran a vcela','3515.jpg'); INSERT INTO "fotografie" VALUES(5,'Kvetina','4174.jpg'); INSERT INTO "fotografie" VALUES(6,'Brambora','4182.jpg');
Přenést obrázky z http://kmlinux/~culik/foto do adresáře static
Soubor controllers.py
from turbogears import controllers, expose, flash from foto import model from turbogears.toolbox.catwalk import CatWalk from model import Fotografie import logging log = logging.getLogger("foto.controllers") class Root(controllers.RootController): catwalk = CatWalk(model) @expose(template="foto.templates.uvod") def index(self): import time log.debug("*** Nekdo navstivil fologalerii ***") flash("Fotografie, verze 01") return dict(current_time=time.ctime()) @expose(template="foto.templates.obsah") def obsah(self): return dict (snimky=Fotografie.select ())
Soubor template/uvod.kid
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="'master.kid'"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/> <title>Fotografie - uvod</title> </head> <body> <div id="sidebar"> <h2>Plovoucí text </h2> Nejaky text <br/> Odkaz na <a href="http://www.turbogears.org">TurboGears website</a> </div> <div class="notice"> <span py:replace="current_time"> tento text bude nahrazen hodnou promenne current_time</span> </div> <div class="commands"> <a href="obsah">Obsah</a> </div> <div class="tools"> <a href="catwalk">Editace tabulky</a> </div> </body> </html>
Soubor template/obsah.kid
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="'master.kid'"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" py:replace="''"/> <title> Fotogalerie - obsah </title> </head> <body> <H2> Fotogalerie </H2> <TABLE WIDTH="100%" border="1"> <TH> Nazev </TH> <TH> Soubor </TH> <TH> Obrazek </TH> <TR py:for="obrazek in snimky"> <TD> ${obrazek.nazev} </TD> <TD> <A HREF='static/${obrazek.soubor}'> ${obrazek.soubor} </A> </TD> <TD> <IMG SRC='static/${obrazek.soubor}' HEIGHT="100"/> </TD> </TR> </TABLE> <div class="tools"> <a href="catwalk">Editace tabulky</a> </div> </body> </html>
Část souboru static/css/style.css
.commands { margin: 0.5em auto 0.5em auto; padding: 15px 10px 15px 55px; width: 450px; background: #ff7 ; border: 1px solid #cce; } .tools { margin: 0.5em auto 0.5em auto; padding: 15px 10px 15px 55px; width: 450px; background: #f77 ; border: 1px solid #cce; }
turbogears.txt · Last modified: 2008/04/02 17:33 by 127.0.0.1