Global context
Django requires that the hypergen.context.context_middleware
middleware is added to the MIDDLEWARE
variable in settings.py:
MIDDLEWARE = [
...
'hypergen.context.context_middleware',
...
]
It adds an immutable global data structure that is local to the request. It's used throughout hypergen to collect html and several other features. Get at it like so:
from hypergen.context import context
print(context.request) # The middleware adds the request.
It's available to users too, although the names 'request' and 'hypergen' are reserved for internal use.
Heres whats in context.hypergen.into
right now for an example:
['<!DOCTYPE ',
'html',
'>',
'',
'',
'',
'',
'<',
'html',
'>',
'',
'',
'',
'',
'<',
'head',
'>',
'<',
'meta',
' ',
'charset',
'="',
'utf-8',
'"',
'/',
'>',
'',
'<',
'meta',
' ',
'http-equiv',
'="',
'X-UA-Compatible',
'"',
' ',
'content',
'="',
'IE=edge',
'"',
'/',
'>',
'',
'<',
'meta',
' ',
'name',
'="',
'viewport',
'"',
' ',
'content',
'="',
'width=device-width, initial-scale=1.0',
'"',
'/',
'>',
'',
'<',
'title',
'>',
'Django Hypergen',
'</title>',
'<',
'link',
' ',
'rel',
'="',
'stylesheet',
'"',
' ',
'type',
'="',
'text/css',
'"',
' ',
'href',
'="',
'https://unpkg.com/simpledotcss@2.0.7/simple.min.css',
'"',
'>',
'</link>',
'<',
'script',
' ',
'src',
'="',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js',
'"',
'>',
'</script>',
'<',
'link',
' ',
'rel',
'="',
'stylesheet',
'"',
' ',
'type',
'="',
'text/css',
'"',
' ',
'href',
'="',
'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.9.1/font/bootstrap-icons.css',
'"',
'>',
'</link>',
'<',
'link',
' ',
'rel',
'="',
'stylesheet',
'"',
' ',
'type',
'="',
'text/css',
'"',
' ',
'href',
'="',
'/static/website/website.77cb245d8ed8.css',
'"',
'>',
'</link>',
'<',
'script',
' ',
'src',
'="',
'/static/website/website.489e44e3c0b0.js',
'"',
' ',
'defer',
'>',
'</script>',
'<',
'link',
' ',
'rel',
'="',
'apple-touch-icon',
'"',
' ',
'sizes',
'="',
'120x120',
'"',
' ',
'type',
'="',
'text/css',
'"',
' ',
'href',
'="',
'/static/apple-touch-icon.e94ab2ed1ca4.png',
'"',
'>',
'</link>',
'<',
'link',
' ',
'rel',
'="',
'icon',
'"',
' ',
'type',
'="',
'image/png',
'"',
' ',
'sizes',
'="',
'32x32',
'"',
' ',
'href',
'="',
'/static/favicon-32x32.aa7d2959144b.png',
'"',
'>',
'</link>',
'<',
'link',
' ',
'rel',
'="',
'icon',
'"',
' ',
'type',
'="',
'image/png',
'"',
' ',
'sizes',
'="',
'16x16',
'"',
' ',
'href',
'="',
'/static/favicon-16x16.6ea060c6ee23.png',
'"',
'>',
'</link>',
'<',
'link',
' ',
'rel',
'="',
'mask-icon',
'"',
' ',
'color',
'="',
'#5bbad5',
'"',
' ',
'type',
'="',
'text/css',
'"',
' ',
'href',
'="',
'/static/safari-pinned-tab.88b908776bf2.svg',
'"',
'>',
'</link>',
'<',
'meta',
' ',
'name',
'="',
'msapplication-TileColor',
'"',
' ',
'content',
'="',
'#da532c',
'"',
'/',
'>',
'',
'<',
'meta',
' ',
'name',
'="',
'theme-color',
'"',
' ',
'content',
'="',
'#ffffff',
'"',
'/',
'>',
'',
'<',
'link',
' ',
'rel',
'="',
'stylesheet',
'"',
' ',
'type',
'="',
'text/css',
'"',
' ',
'href',
'="',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css',
'"',
'>',
'</link>',
'</head>',
'',
'',
'',
'',
'<',
'body',
'>',
'',
'',
'',
'',
'<',
'header',
'>',
'',
'',
'',
'',
'<',
'nav',
'>',
'<',
'a',
' ',
'href',
'="',
'/',
'"',
'>',
'Home',
'</a>',
'<',
'a',
' ',
'href',
'="',
'/documentation/',
'"',
'>',
'Documentation',
'</a>',
'<',
'a',
' ',
'href',
'="',
'/news/',
'"',
'>',
'News',
'</a>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'a',
' ',
'href',
'="',
'https://github.com/runekaagaard/django-hypergen/',
'"',
'>',
'<',
'img',
' ',
'src',
'="',
'/static/website/github.f87561b8bb35.png',
'"',
' ',
'class',
'="',
'icon',
'"',
'/',
'>',
'',
'Github',
'</a>',
'</nav>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'div',
' ',
'class',
'="',
'title',
'"',
'>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'h1',
'>',
img(src="/static/website/hypergen-logo.bcf5accd9cb7.png", class_="logo"),
'ypergen',
'</h1>',
'<',
'span',
'>',
' - take a break from javascript',
'</span>',
'</div>',
'</header>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'main',
' ',
'id',
'="',
'main',
'"',
'>',
'',
'',
'',
'',
'<',
'p',
'>',
'<',
'a',
' ',
'href',
'="',
'/documentation/',
'"',
'>',
'Back to documentation',
'</a>',
'</p>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'div',
' ',
'id',
'="',
'content',
'"',
'>',
'<',
'h2',
'>',
'Global context',
'</h2>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'p',
'>',
'Django requires that the',
' ',
code("hypergen.context.context_middleware"),
' ',
'middleware is added to the',
' ',
code("MIDDLEWARE"),
' ',
'variable in settings.py:',
'</p>',
'',
'',
'',
'',
'',
'<',
'pre',
'>',
code("MIDDLEWARE = [
...
'hypergen.context.context_middleware',
...
]"),
'</pre>',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'<',
'p',
'>',
'It adds an immutable global ',
a("data structure", href="https://pyrsistent.readthedocs.io/en/latest/api.html#pyrsistent.pmap"),
' that is local to the request. ',
'It's used throughout hypergen to collect html and several other '
'features. Get at it like so:',
'</p>',
'',
'',
'',
'',
'',
'<',
'pre',
'>',
code("from hypergen.context import context
print(context.request) # The middleware adds the request."),
'</pre>',
'<',
'p',
'>',
'It's available to users too, although the names 'request' and '
''hypergen' are reserved for internal use.',
'</p>',
'',
'',
'',
'',
'',
'<',
'p',
'>',
'Heres whats in',
' ',
code("context.hypergen.into"),
' ',
'right now for an example:',
'</p>']
Using global context in your own apps
You can use your own global context like this example:
def context_example():
with context(at="my_appname", title="my original items", items=[1, 2, 3]):
with dl():
dt(context["my_appname"]["title"])
dd(context["my_appname"]["items"], sep=", ")
with context(at="my_appname", title="my nested items", items=[4, 5]):
dt(context["my_appname"]["title"])
dd(context["my_appname"]["items"], sep=", ")
dt(context.my_appname["title"])
dd(context.my_appname["items"], sep=", ")
And it would yield the following html:
- my original items
- 1, 2, 3
- my nested items
- 4, 5
- my original items
- 1, 2, 3