Add documentation and source

Added documentation, source and extra files.
This commit is contained in:
2025-07-02 20:26:50 +02:00
parent 4fc1d36a10
commit e3ebf6bf4f
295 changed files with 24986 additions and 0 deletions

View File

@ -0,0 +1,9 @@
from django.apps import AppConfig
class DemonstrationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'demonstration'
default_app_config = "demonstration.DemonstrationConfig"

View File

@ -0,0 +1,48 @@
:root {
--head-bg-color: #222;
--head-fg-color: #fff;
--head-ln-color: #3cf;
}
html, body {
height: 100%;
min-height: 100%;
width: 100%;
}
body {
margin: 0;
display: grid;
grid-template-areas: "header" "content" "footer";
grid-template-rows: auto 1fr auto;
grid-template-columns: 100%;
font-family: "Roboto", "Lucida Grande", "DejaVu Sans", "Bitstream Vera Sans", Verdana, Arial, sans-serif;
}
section#header {
grid-area: header;
}
section#content {
grid-area: content;
}
section#footer {
grid-area: footer;
}
section#header, section#footer {
background-color: var(--head-bg-color);
color: var(--head-fg-color);
}
section#header a, section#footer a {
color: var(--head-ln-color);
text-decoration: none;
}
div.body {
width: 1024px;
margin: 1.5em auto;
}

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Template with control structures</title>
</head>
<body>
Use of a control structure to parse a list of values:
<ul>
{% for item in values %}
{% if item != 3 %} {# Do not show the item with a value of 3 #}
<li>Item n.{{ forloop.counter }}: {{ item }}</li>
{% endif %}
{% endfor %}
</ul>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Template with filtering</title>
</head>
<body>
<p>Use different filters on values:</p>
{# Filter for strings #}
<ul>
{% for item in strings %}
<li>{{ item|title }}</li>
{% endfor %}
</ul>
{# Filter for dates #}
<p>{{ moment|date:"Y m d" }}</p>
</body>
</html>

View File

@ -0,0 +1,7 @@
This is a plain text file rendered as a template.
Django can use any text file to render a template, whether it's an XML, HTML, RST or TXT file.
The only specificity in rendering a template is that we can use values, control structures and
a few tricks to make the rendering more dynamic.
Templates are generally used in Django to render HTML, text and custom email bodies.

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Template with variables</title>
</head>
<body>
This is an HTML file rendered as a template, with interpolated variables.
Django can use any text file to render a template, whether it's an XML, HTML, RST or TXT file.
The only specificity in rendering a template is that we can use values, control structures and
a few tricks to make the rendering more dynamic.
Templates are generally used in Django to render HTML, text and custom email bodies.
Variables passed to the template context:
<ul>
<li>Variable1: {{ variable1 }}</li>
<li>Variable2: {{ variable2 }}</li>
</ul>
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Templating demonstration</title>
</head>
<body>
<h1>Sections of templating</h1>
<ul>
<li><a href="{% url "untemplated" %}">No template, just an HttpResponse</a></li>
<li><a href="{% url "basic-plain" %}">Basic: plain text</a></li>
<li><a href="{% url "basic-variables" %}">Basic: HTML with variables</a></li>
<li><a href="{% url "basic-control" %}">Basic: HTML with control structures</a></li>
<li><a href="{% url "basic-control-class" %}">Basic: HTML with control structures (class-based)</a></li>
<li><a href="{% url "basic-filters" %}">Basic: HTML with template filters</a></li>
<li><a href="{% url "inheritance-base" %}">Inheritance: Base template with overridable blocks</a></li>
</ul>
</body>
</html>

View File

@ -0,0 +1,33 @@
{% load static %} {# The load tag enables template tags from other Django apps #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} | Django Demonstration{% endblock title %}</title>
<link rel="stylesheet" href="{% static "demonstration/demonstration.css" %}">
</head>
<body>
<section id="header">
<div class="body">
<nav>
<a href="{% url "index" %}">Home page</a>
</nav>
</div>
</section>
<section id="content">
<div class="body">
{% block body %}
Base content in a overridable block.
{% endblock body %}
</div>
</section>
<section id="footer">
<div class="body">
{% block footer %}
©2021 Steve Kossouho, <strong>Dawan</strong>
{% endblock footer %}
</div>
</section>
</body>
</html>

View File

@ -0,0 +1,8 @@
{% extends "demonstration/inheritance/base.html" %}
{% load static %} {# The load tag enables template tags from other Django apps #}
{% block title %}Inherited page{{ block.super }}{% endblock title %}
{% block body %}
Change body block from the original in the overridden block.
{% endblock body %}

View File

@ -0,0 +1,9 @@
{% extends "demonstration/inheritance/base.html" %}
{% load i18n %} {# The i18n tag enables template tags for internationalization #}
{% block title %}{% translate "Inherited page" %}{{ block.super }}{% endblock title %}
{% block body %}
<p>{% translate "First string for translation." %}</p>
<p>{% blocktranslate %}Second string with placeholder: {{ value }}{% endblocktranslate %}</p>
{% endblock body %}

View File

@ -0,0 +1,36 @@
from datetime import datetime
from django import template
from django.template.defaultfilters import stringfilter
from pytz import UTC
register = template.Library()
@register.filter
@stringfilter
def decorate(value, _arg=None):
"""
Template filter to decorate the value with hyphens.
Args:
value: Any value that can be displayed and filtered.
_arg: Option passed for the filter, if any
Returns:
Decorated value, like --<value>--
"""
return f"--{value}--"
@register.simple_tag
def current_time():
"""
Template tag that returns the current time, with the UTC timezone.
Returns:
Now in UTC.
"""
return datetime.now(tz=UTC)

View File

@ -0,0 +1,14 @@
from django.urls import path
from demonstration.views import *
urlpatterns = [
path("untemplated/", view_untemplated, name="untemplated"),
path("basic/plain/", view_basic_plain, name="basic-plain"),
path("basic/variables/", view_basic_variables, name="basic-variables"),
path("basic/control/", view_basic_control, name="basic-control"),
path("basic/control/class/", BasicTemplateView.as_view(), name="basic-control-class"),
path("basic/filters/", view_basic_filters, name="basic-filters"),
path("inheritance/base/", view_inheritance_base, name="inheritance-base"),
]

View File

@ -0,0 +1,3 @@
from .index import *
from .basic import *
from .inheritance import *

View File

@ -0,0 +1,55 @@
from annoying.decorators import render_to
from django.http import HttpRequest, HttpResponse
from django.views.generic import TemplateView
from pytz import UTC
def view_untemplated(request: HttpRequest) -> HttpResponse:
"""Return a base view for demonstration purposes."""
return HttpResponse("Page content returned")
@render_to("demonstration/basic/plain.txt")
def view_basic_plain(request: HttpRequest) -> dict:
"""Show a plain text rendered as a template."""
return {}
@render_to("demonstration/basic/variables.html")
def view_basic_variables(request: HttpRequest) -> dict:
"""Show a template using variable interpolation."""
return {"variable1": "Text number 1.", "variable2": 599.31}
@render_to("demonstration/basic/control.html")
def view_basic_control(request: HttpRequest) -> dict:
"""Use control structures."""
return {"values": [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]}
@render_to("demonstration/basic/filters.html")
def view_basic_filters(request: HttpRequest) -> dict:
"""Use template filters."""
from datetime import datetime
return {"strings": ["text in lowercase", "basic text."], "moment": datetime(2020, 1, 1, tzinfo=UTC)}
@render_to("demonstration/basic/tags.html")
def view_basic_tags(request: HttpRequest) -> dict:
"""Use template tags."""
return {"numbers": [1, 2, 3, 4, 5, 6, 7, 8]}
class BasicTemplateView(TemplateView):
"""
Equivalent of the `view_basic_control` view.
See Also:
Documentation to know how to use class-based views:
- https://docs.djangoproject.com/fr/3.2/topics/class-based-views/
"""
template_name = "demonstration/basic/control.html"
extra_context = {"values": [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]}

View File

@ -0,0 +1,8 @@
from annoying.decorators import render_to
from django.http import HttpRequest
@render_to("demonstration/index.html")
def view_index(request: HttpRequest) -> dict:
"""Show the list of all sections."""
return {}

View File

@ -0,0 +1,8 @@
from annoying.decorators import render_to
from django.http import HttpRequest
@render_to("demonstration/inheritance/base.html")
def view_inheritance_base(request: HttpRequest) -> dict:
"""Show the list of all sections."""
return {}