/ / HTML templating usando Jinja2 - Lost - python, html, templates, jinja2

Modelli HTML usando Jinja2 - Lost - python, html, templates, jinja2

Sto provando a creare un modello html in python usando Jinja2. Ho una cartella di modelli con il mio "template.html" ma non so come gestire ambienti o caricatori di pacchetti.

Ho installato Jinja2 usando easy_python e ho eseguito il seguente script.

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader("yourapplication", "templates"))
template = env.get_template("mytemplate.html")
print template.render()

Ottengo il seguente errore perché non so come definire un pacchetto / modulo. Per favore aiutami Voglio solo creare un modello semplice.

  File "log_manipulationLL.py", line 291, in <module>
env = Environment(loader=PackageLoader("yourapplication", "templates"))
File "/usr/local/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg/jinja2/loaders.py",    line 216, in __init__
provider = get_provider(package_name)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 213, in get_provider
__import__(moduleOrReq)
ImportError: No module named yourapplication

risposte:

8 per risposta № 1

PackageLoader si aspetta un vero modulo Python usando la normale sintassi del punto. Ad esempio se la tua struttura assomiglia a questa:

myapp/
__init__.py
…
templates/
mytemplate.html

Dovresti usare myapp come il nome del modulo.


9 per risposta № 2

Se non vuoi o hai bisogno di un pacchetto Python, probabilmente dovresti usare a FileSystemLoader invece, così:

from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader("file/path/"),
autoescape=select_autoescape(["html", "xml"]),
)

8 per risposta № 3

Ho risolto questo problema utilizzando il seguente codice:

 env = Environment(loader=PackageLoader("scriptname",
templatesPath))

dove questo codice è nel file scriptname.py.

Non sono sicuro che la mia risposta sia pertinente, ma mi stavo chiedendo che forse qualcuno potrebbe trovare utile questa risposta. Se sbaglio, fammi sapere.


2 per risposta № 4

PackageLoader è definito in questo modo:

class PackageLoader(BaseLoader):
"""Load templates from python eggs or packages.  It is constructed with
the name of the python package and the path to the templates in that
package::

loader = PackageLoader("mypackage", "views")

If the package path is not given, ``"templates"`` is assumed.

Per default the template encoding is ``"utf-8"`` which can be changed
by setting the `encoding` parameter to something else.  Due to the nature
of eggs it"s only possible to reload templates if the package was loaded
from the file system and not a zip file.
"""

E poi il __init__() il metodo è il seguente:

def __init__(self, package_name, package_path="templates",
encoding="utf-8"):

Questo ci fa notare che una struttura come questa:

myapp/
__init__.py
...
templates/
mytemplate.html

Avrà lo stesso PackageLoader istanza con entrambe queste dichiarazioni:

PackageLoader("myapp")
PackageLoader("myapp", "templates")

E quindi se stai scappando dal myapp/ percorso, devi solo dire:

PackageLoader("templates", "")

In modo che prenderà solo templates/ come il percorso. Se lasci vuoto il secondo argomento, cercherà di trovare i modelli in templates/templates.

Infine, puoi controllare cosa è stato caricato usando il list_templates() metodo:

PackageLoader("templates", "").list_templates()