File: django_template_render.py

package info (click to toggle)
python-pyinstrument 5.1.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,624 kB
  • sloc: python: 6,713; ansic: 897; makefile: 46; sh: 26; javascript: 18
file content (50 lines) | stat: -rw-r--r-- 1,097 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
from optparse import OptionParser

try:
    import django
except ImportError:
    print("This example requires Django.")
    print("Install using `pip install Django`.")
    exit(1)

import django.conf
import django.template.loader


def main():
    parser = OptionParser()
    parser.add_option(
        "-i",
        "--iterations",
        dest="iterations",
        action="store",
        type="int",
        help="number of template render calls to make",
        default=200,
    )
    options, _ = parser.parse_args()

    os.chdir(os.path.dirname(__file__))

    django.conf.settings.configure(
        INSTALLED_APPS=(),
        TEMPLATES=[
            {
                "BACKEND": "django.template.backends.django.DjangoTemplates",
                "DIRS": ["./django_example/django_example/templates"],
            }
        ],
    )
    django.setup()

    render_templates(options.iterations)


def render_templates(iterations: int):
    for _ in range(0, iterations):
        django.template.loader.render_to_string("template.html")


if __name__ == "__main__":
    main()