- Mandate a banner on any page base.html
{% if cookies_check() %}
{# then user has already consented so no requirement for consent banner #}
{% else %}
{# show a cookie consent banner #}
<div id="cookie-consent-container">
<button id="cookie-consent">I Consent</button>
</div>
<script>
var fn = function () {
document.cookie = "cookie_consent=true";
document.getElementById('cookie-consent-container').hidden = true;
};
document.getElementById('cookie-consent').onclick = fn;
</script>
{% endif %}
- Inject the function into jijna2 to check the cookies
@app.app_context_processor
def inject_template_scope():
injections = dict()
def cookies_check():
value = request.cookies.get('cookie_consent')
return value == 'true'
injections.update(cookies_check=cookies_check)
return injections
It works.