How to get permission for cookies from user in Flask How to get permission for cookies from user in Flask flask flask

How to get permission for cookies from user in Flask


For what its worth I did this:

  1. 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 #}I Consentvar fn = function () {document.cookie = "cookie_consent=true";document.getElementById('cookie-consent-container').hidden = true;};document.getElementById('cookie-consent').onclick = fn;{% endif %}

  2. Inject the function into jijna2 to check the cookies:

    @app.context_processordef 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

I also used the dev console to detect existing cookies by exploring document.cookies. It seemed the only cookies initially generated were Google Analytics.