Ich habe folgendes Problem: Ich möchte das Template-Framework Mako verwenden und bin wie folgt vorgegangen, er erkennt alles prima, nur wenn er die Listenwerte ausgeben soll, macht er was er will.
Code: Alles auswählen
import codecs
import os.path
import string
from mako.template import Template
from mako.lookup import TemplateLookup
#----------------------------------------------------------
class View_cl(object):
#----------------------------------------------------------
#-------------------------------------------------------
def __init__(self, path_spl):
#-------------------------------------------------------
self.path_s = os.path.join(path_spl, "template")
self.lookup_o = TemplateLookup(directories=[self.path_s])
#-------------------------------------------------------
def create_p(self, template_spl, data_opl):
template_o = self.lookup_o.get_template(template_spl)
return template_o.render(data_o = data_opl)
def createList_px(self, data_opl):
#Templategenerierung der Firmendaten
return self.create_p('liste.tpl', data_opl)
Code: Alles auswählen
def save(self, **data_opl):
# Savefunktion für das Firmenverzeichnis
# Sichern der Daten: aufgrund der Formularbearbeitung muss
# eine vollständige HTML-Seite zurückgeliefert werden!
# data_opl: Dictionary mit den gelieferten key-value-Paaren
# hier müsste man prüfen, ob die Daten korrekt vorliegen!
id_s = data_opl["id_s"]
data_a =[ data_opl["firmenname"]
, data_opl["branche"]
, data_opl["taetigkeit"]
, data_opl["sitz"]
, data_opl["anzahl"]
]
## coding: utf-8
<table id="idList">
<tr><th>Firmenname</th><th>Branche</th><th>Taetigkeit</th><th>Sitz</th><th>Anzahl</th></tr>
## man verwendet hier Zugriff auf das Dictionary "data_o"
% for id_s in data_o:
<tr id="r${id_s}">
<td>${data_o[id_s]['firmenname']}</td>
<td>${data_o[id_s]['branche']}</td>
<td>${data_o[id_s]['taetigkeit']}</td>
<td>${data_o[id_s]['sitz']}</td>
<td>${data_o[id_s]['anzahl']}</td>
</tr>
% endfor
[/code]
Wenn ich denn die Seite besuchen will, in der er die Template-Engine erzeugen möchte gibt er mir folgenden Fehler aus. Ich habe darauf geachtet die Listenwerte gesondert auszugeben mittlerweile weiß ich echt nichtmehr weiter und die Dokumentationen von Mako hilft auch nicht sehr viel.
Hier nochmal das komplette Projekte für die Tiefeneinsicht.
127.0.0.1 - - [02/Nov/2016:21:04:29] "GET /praxisphasenangebot HTTP/1.1" 200 6979 "http://127.0.0.1:8080/" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
[02/Nov/2016:21:04:34] HTTP
Traceback (most recent call last):
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cherrypy-8.1.2-py3.5.egg\cherrypy\_cprequest.py", line 670, in respond
response.body = self.handler()
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cherrypy-8.1.2-py3.5.egg\cherrypy\lib\encoding.py", line 220, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\cherrypy-8.1.2-py3.5.egg\cherrypy\_cpdispatch.py", line 60, in __call__
return self.callable(*self.args, **self.kwargs)
File "C:\Users\thomas\PycharmProjects\web_prak2\p2\ppm\app\application.py", line 29, in firmenverwaltung
return self.createList_p()
File "C:\Users\thomas\PycharmProjects\web_prak2\p2\ppm\app\application.py", line 159, in createList_p
return self.view_o.createList_px(data_o)
File "C:\Users\thomas\PycharmProjects\web_prak2\p2\ppm\app\view.py", line 36, in createList_px
return self.create_p('liste.tpl', data_opl)
File "C:\Users\thomas\PycharmProjects\web_prak2\p2\ppm\app\view.py", line 31, in create_p
return template_o.render(data_o = data_opl)
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mako-1.0.4-py3.5.egg\mako\template.py", line 445, in render
return runtime._render(self, self.callable_, args, data)
127.0.0.1 - - [02/Nov/2016:21:04:34] "GET /firmenverwaltung HTTP/1.1" 500 823 "http://127.0.0.1:8080/praxisphasenangebot" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0"
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mako-1.0.4-py3.5.egg\mako\runtime.py", line 829, in _render
**_kwargs_for_callable(callable_, data))
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mako-1.0.4-py3.5.egg\mako\runtime.py", line 864, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "C:\Users\thomas\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mako-1.0.4-py3.5.egg\mako\runtime.py", line 890, in _exec_template
callable_(context, *args, **kwargs)
File "liste_tpl", line 26, in render_body
TypeError: list indices must be integers or slices, not str
[02/Nov/2016:21:04:34] HTTP
Request Headers:
ACCEPT-LANGUAGE: de,en-US;q=0.7,en;q=0.3
ACCEPT-ENCODING: gzip, deflate
USER-AGENT: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
CONNECTION: keep-alive
ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HOST: 127.0.0.1:8080
Remote-Addr: 127.0.0.1
REFERER: http://127.0.0.1:8080/praxisphasenangebot
UPGRADE-INSECURE-REQUESTS: 1
https://hs-niederrhein.sciebo.de/index. ... Vp399W712v