Der Grundgedanke einer Vorlagensprache ist, dass die Sprache in ein anderes Masterdokument „eingebettet“ ist. Es handelt sich um eine Sprache, die die Definition von Platzhaltern ermöglicht, die später im Entwurfsprozess ersetzt werden sollen. Vorlagen ermöglichen die Definition von Platzhaltern, die später ausgefüllt und durch tatsächliche Inhalte ersetzt werden. Die meisten modernen, webbasierten Templatesprachen bieten eine Vielzahl von Funktionen. So ist es zum Beispiel möglich, innerhalb einer Vorlage eine Schleife durch Informationen zu ziehen und eine bedingte Logik bereitzustellen!
Mako ist eine solche Templating-Sprache, die darauf ausgelegt ist, sowohl die Leistung als auch die Einfachheit der Syntax zu maximieren. Ein Hauptmerkmal von Mako ist, dass es die Vertrautheit mit anderen Templatesprachen bietet, aber Verbesserungen an ihnen vornimmt, um den resultierenden Code so lesbar wie möglich zu machen. Mako erlaubt es Ihnen auch, Python-Logik innerhalb der Templates zu schreiben, was es zu einer der am besten gestalteten Templating-Sprachen macht. In diesem Artikel werden wir uns ansehen, wie wir Mako mit Django integrieren können und auch einige der Fehler betrachten, auf die man dabei stößt.
Installation und Einrichtung
Beginnen wir mit der Erstellung des Django-Projekts und passen es anschließend an die Mako-Vorlagen an. Installieren Sie zunächst die folgenden Voraussetzungen, um fortzufahren:
pip install djangomako
Der nächste Schritt besteht darin, ein Django-Projekt und eine Beispielanwendung zu erstellen:
django-admin startproject makoprojectcd makoproject
python manage.py startapp makoapp
Jetzt können wir unser Einstellungs-Backend für die Verwendung von Mako anpassen; das Schreiben eines benutzerdefinierten Backends ist ziemlich einfach. Wir brauchen eine Klasse, die von unserer BaseEngine erbt und die Template-Objekte zurückgibt. Innerhalb dieser Template-Klassen haben wir die Kontrolle über die Logik und das Rendering von Inhalten, genau so, wie wir es wollen.
Fügen Sie zunächst „djangomako“ und den Namen Ihrer Anwendung zur Liste der installierten Anwendungen hinzu:
# settings.py
INSTALLED_APPS = [
# ...
'djangomako',
'makoApp',
]
Fügen Sie dann den unten angegebenen Code ganz unten im Abschnitt Vorlagen ein:
# settings.py
import os
TEMPLATES = [ # ...
{
'BACKEND': 'djangomako.backends.MakoBackend',
'NAME': 'mako',
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
},]
Der nächste Schritt ist die Anpassung des Pakets djangomako für unser Projekt, da es den alten Import für die statischen Dateien hat. Um dies zu beheben, müssen wir unser Paket djangomako finden. Ein einfacher Weg, dies zu tun, ist, den Projektserver mit zu starten: ‚python manage.py runserver‘ und es wird eine Fehlermeldung ausgegeben, in einer dieser Zeilen wird der Ort der Datei mit dem Namen ‚backend.py‘ stehen. Dies würde in etwa so aussehen:

Hier befindet sich der falsche Import. Der Speicherort dieser Datei ist bei jedem System anders, so dass Sie selbst dorthin navigieren müssen. Wenn Sie VScode verwenden, können Sie auch einfach auf die Zeile mit dem Speicherort klicken und die Datei wird geöffnet.
Gehen Sie nun in der Datei backends.py zu Zeile 14, wie im Bild oben vorgeschlagen, und ändern Sie diese Zeile durch die unten angegebene:
#Old import
from django.contrib.staticfiles.templatetags.staticfiles import static
#New import from django.templatetags.static import static
Wenn Sie danach in derselben Datei in Zeile 204 einen Fehler erhalten, löschen Sie diese Zeile und ersetzen Sie sie durch die folgende Zeile:
#Old import
from django.core.urlresolvers import reverse
#New import
from django.urls import reverse
Als abschließende Kontrolle, ob alles funktioniert hat, starten Sie den Server erneut:
python3 manage.py runserver #Linux/Mac
python manage.py runserver #Windows
Wenn wir jetzt unseren lokalen Server starten, wird alles reibungslos ablaufen. Wenn Sie die volle Kontrolle über dieses Paket haben wollen, können Sie auch einen Ordner mit einer init-Datei im Hauptverzeichnis erstellen. Kopieren Sie backends.py dorthin und es wird als Python-Modul funktionieren!
Als nächstes müssen Sie die URLs in der Datei makoproject/urls.py einrichten:
# makoproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('makoApp.urls'))
]
Benutzung der Bibliothek
Jetzt können wir endlich die Mako-Bibliothek in unserer Anwendung verwenden, aber zuerst müssen wir die URLs dafür einrichten. Erstellen Sie dazu eine Datei namens urls.py im Verzeichnis Ihrer Anwendung (makoapp) und fügen Sie die folgenden Zeilen ein:
# makoapp/urls.py
from django.urls import path
from .views import *
urlpatterns = [
path('', MakoView.as_view(), name='home'),
]
Öffnen Sie dann die Datei views.py und fügen Sie den folgenden Code hinzu; dies ist eine einfache Funktion, die nur einige vordefinierte Namen anzeigt, obwohl sie auch durch die Modelle oder Formulare geleitet werden könnte.
# views.py
from django.shortcuts import render
from django.views.generic import TemplateView #get the mako template to views
class MakoView(TemplateView):
template_name = "home.mako" #define the template name
def get_context_data(self, **kwargs):
context = super(MakoView, self).get_context_data(**kwargs) #get the makoview to the thempltes
context.update({
'names': [
'User',
],
})
return context
Zu guter Letzt müssen Sie ein Verzeichnis „templates“ auf der Stammebene unseres Projekts anlegen und darin eine Datei „home.mako“ erstellen (Sie können aber auch die Erweiterung .html verwenden). Danach können Sie den folgenden Code in die Datei einfügen:
#templates/home.mako
#Example to showcase the view function
<p>Welcome to mako template</p>
<ul>
% for name in names:
<li>${ name }</li>
% endfor
</ul>
#Example to show Mako's integrated capablities
<%
from random import randint
numbers = [randint(1,10) for _ in range(10)]
%>
<ul>
% for n in numbers:
<li>${n}</li>
% endfor
</ul>
Und wir sind fertig!
Wie Sie sehen können, wird das erste Beispiel die Funktion ausgeben, die wir in den Ansichten erstellt haben und die den vordefinierten Namen ausgeben soll. Aber das ist nichts Besonderes, DTL könnte das auch tun.
Im zweiten Beispiel ist jedoch ein beliebiger Python-Code in die Vorlage eingefügt. Es handelt sich um ein einfaches Programm, das 10 Zufallszahlen ausdruckt und sie über den HTML-Tag in die Vorlage überträgt.
Ergebnisse
Nachdem Sie alle oben genannten Schritte durchgeführt haben, können Sie nun den Django-Entwicklungs-Webserver starten, um Ihre Ergebnisse anzuzeigen. Sie können den Server mit dem folgenden Befehl starten:
python3 manage.py runserver #Linux/Mac
python manage.py runserver #Windows
Wenn der Server erfolgreich gestartet wurde, besuchen Sie den angegebenen Link, d. h. diesen http://127.0.0.1:8000/. Wenn alles gut funktioniert hat, sehen Sie eine Seite wie diese:

Schlussfolgerung
Mako ist einfach zu erlernen, aber es ist auch eine extrem leistungsfähige Template-Engine. Sie kann die volle Kraft von Django entfesseln, um dynamische, inhaltsreiche Seiten zu erstellen. Aber es gibt einige Dinge, die Sie lernen müssen, um es zu benutzen, wie zum Beispiel die Tags. Aber die vielen Funktionen, die es bietet, reichen aus, um viele Leute dazu zu bringen, es zu benutzen.
In diesem Artikel haben wir uns angeschaut, wie wir Mako mit Django integrieren können und auch einige der Fehler, auf die Leute dabei stoßen, betrachtet. Wir hoffen, dass dieser Artikel Ihnen bei der Integration von Mako mit Django geholfen hat. Wenn Sie weitere Fragen oder Bedenken zu diesem Thema haben, können Sie uns jederzeit kontaktieren. Wenn Sie auf der Suche nach einer Templatesprache für Ihr nächstes Projekt sind, empfehlen wir Ihnen einen Blick auf Mako zu werfen.
Dein Kommentar wartet auf die Überprüfung durch einen Moderator.
Trailer trucks queue to cross into the United States at the Otay Mesa Port of Entry, in Tijuana, Mexico, November 27, 2024. Jorge Duenes/Reuters
New York
CNN
—
[url=https://bs2bot.shop]блэк спрут ссылка[/url]
Since President Donald Trump won the election in November, businesses across the globe have been bracing for higher tariffs — a key Day One promise the president made.
But over a week into his presidency, Trump has yet to enact any new tariffs.
[url=https://bsme.me]спрут зеркало[/url]
That could change, come 11:59 p.m. ET on Saturday — the deadline Trump set for when he says he will slap 25% tariffs on all Mexican and Canadian goods and a 10% tariff on all Chinese goods.
The tariffs, he said, will be imposed as a way of punishing the three nations, which Trump claims are responsible for helping people enter the country illegally and supplying fentanyl consumed in the US.
Speaking to reporters from the Oval Office on Thursday, Trump said he meant business, especially with his tariff threats on Mexico and Canada. White House Press Secretary Karoline Leavitt also confirmed on Friday that Trump will levy the 10% tariff on China on Saturday.
[url=https://bs2web.de]bs2best at[/url]
Should these threats be believed? Yes and no, said Trump’s former Commerce Secretary Wilbur Ross.
блэк спрут onion
https://bsmeat.com
The threat of blanket tariffs is likely being overstated, Ross said in an interview with CNN. “There probably will be exclusions, because there are some goods that just are not made here, will not be made here, and therefore, there’s no particular point putting tariffs on.”
Ross, who was one of a handful of initial cabinet members in Trump’s first administration who kept their position for the entire four-year term, said he advocated for such exclusions when he advised Trump on tariff policies.
[url=https://bs2web.ac]сайт спрут[/url]
Dein Kommentar wartet auf die Überprüfung durch einen Moderator.
Trailer trucks queue to cross into the United States at the Otay Mesa Port of Entry, in Tijuana, Mexico, November 27, 2024. Jorge Duenes/Reuters
New York
CNN
—
[url=https://blsp2web.net]blacksprut2rprrt3aoigwh7zftiprzqyqynzz2eiimmwmykw7wkpyad onion[/url]
Since President Donald Trump won the election in November, businesses across the globe have been bracing for higher tariffs — a key Day One promise the president made.
But over a week into his presidency, Trump has yet to enact any new tariffs.
[url=https://bs2shop.org]blacksprut[/url]
That could change, come 11:59 p.m. ET on Saturday — the deadline Trump set for when he says he will slap 25% tariffs on all Mexican and Canadian goods and a 10% tariff on all Chinese goods.
The tariffs, he said, will be imposed as a way of punishing the three nations, which Trump claims are responsible for helping people enter the country illegally and supplying fentanyl consumed in the US.
Speaking to reporters from the Oval Office on Thursday, Trump said he meant business, especially with his tariff threats on Mexico and Canada. White House Press Secretary Karoline Leavitt also confirmed on Friday that Trump will levy the 10% tariff on China on Saturday.
[url=https://bsme.ac]bslp at[/url]
Should these threats be believed? Yes and no, said Trump’s former Commerce Secretary Wilbur Ross.
блекспрут
https://blsprut.tech
The threat of blanket tariffs is likely being overstated, Ross said in an interview with CNN. “There probably will be exclusions, because there are some goods that just are not made here, will not be made here, and therefore, there’s no particular point putting tariffs on.”
Ross, who was one of a handful of initial cabinet members in Trump’s first administration who kept their position for the entire four-year term, said he advocated for such exclusions when he advised Trump on tariff policies.
[url=https://mbs2bestat.ru]блекспрут[/url]
Dein Kommentar wartet auf die Überprüfung durch einen Moderator.
Trailer trucks queue to cross into the United States at the Otay Mesa Port of Entry, in Tijuana, Mexico, November 27, 2024. Jorge Duenes/Reuters
New York
CNN
—
[url=https://blackspfgh3bi6im374fgl54qliir6to37txpkkd6ucfiu7whfy2odid.org]блэк спрут официальный сайт[/url]
Since President Donald Trump won the election in November, businesses across the globe have been bracing for higher tariffs — a key Day One promise the president made.
But over a week into his presidency, Trump has yet to enact any new tariffs.
[url=https://bsme.sh]blacksprut[/url]
That could change, come 11:59 p.m. ET on Saturday — the deadline Trump set for when he says he will slap 25% tariffs on all Mexican and Canadian goods and a 10% tariff on all Chinese goods.
The tariffs, he said, will be imposed as a way of punishing the three nations, which Trump claims are responsible for helping people enter the country illegally and supplying fentanyl consumed in the US.
Speaking to reporters from the Oval Office on Thursday, Trump said he meant business, especially with his tariff threats on Mexico and Canada. White House Press Secretary Karoline Leavitt also confirmed on Friday that Trump will levy the 10% tariff on China on Saturday.
[url=https://blspr2web.net]спрут зеркало[/url]
Should these threats be believed? Yes and no, said Trump’s former Commerce Secretary Wilbur Ross.
блэк спрут
https://bs-me.at
The threat of blanket tariffs is likely being overstated, Ross said in an interview with CNN. “There probably will be exclusions, because there are some goods that just are not made here, will not be made here, and therefore, there’s no particular point putting tariffs on.”
Ross, who was one of a handful of initial cabinet members in Trump’s first administration who kept their position for the entire four-year term, said he advocated for such exclusions when he advised Trump on tariff policies.
[url=https://bsme-at.ru]bslp.at[/url]
Dein Kommentar wartet auf die Überprüfung durch einen Moderator.
Trailer trucks queue to cross into the United States at the Otay Mesa Port of Entry, in Tijuana, Mexico, November 27, 2024. Jorge Duenes/Reuters
New York
CNN
—
[url=https://bsme-at.net]блэкспрут ссылка[/url]
Since President Donald Trump won the election in November, businesses across the globe have been bracing for higher tariffs — a key Day One promise the president made.
But over a week into his presidency, Trump has yet to enact any new tariffs.
[url=https://blacksprut2rprrt3aoigwh7zftiprzqyqynzz2eiimmwmykw7wkpyad.com]блекспрут[/url]
That could change, come 11:59 p.m. ET on Saturday — the deadline Trump set for when he says he will slap 25% tariffs on all Mexican and Canadian goods and a 10% tariff on all Chinese goods.
The tariffs, he said, will be imposed as a way of punishing the three nations, which Trump claims are responsible for helping people enter the country illegally and supplying fentanyl consumed in the US.
Speaking to reporters from the Oval Office on Thursday, Trump said he meant business, especially with his tariff threats on Mexico and Canada. White House Press Secretary Karoline Leavitt also confirmed on Friday that Trump will levy the 10% tariff on China on Saturday.
[url=https://blackspfgh3bi6im374fgl54qliir6to37txpkkd6ucfiu7whfy2odid.org]bsme .at[/url]
Should these threats be believed? Yes and no, said Trump’s former Commerce Secretary Wilbur Ross.
bsme.at
https://btrhbfeojofxcpxuwnsp5h7h22htohw4btqegnxatocbkgdlfiawhyid.org
The threat of blanket tariffs is likely being overstated, Ross said in an interview with CNN. “There probably will be exclusions, because there are some goods that just are not made here, will not be made here, and therefore, there’s no particular point putting tariffs on.”
Ross, who was one of a handful of initial cabinet members in Trump’s first administration who kept their position for the entire four-year term, said he advocated for such exclusions when he advised Trump on tariff policies.
[url=https://bs-bot.net]блэк спрут официальный сайт[/url]