I am using Jinja2 version 2.10 with Django. I get a not found error for my css stylesheet. I have read every question on Stackoverflow related to Jinja2 templating in Django and tried all the answers, still no success.
My jinja2 config file (top level in my Django project):
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
from jinja2 import Environment
# This enables us to use Django template tags like {% url ‘index’ %} or {% static ‘path/to/static/file.js’ %} in our Jinja2 templates.
def environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
My html templates load as expected and the page shows in the browser, it's just the css load that isn't working. My folder structure is:
Django_project
-- my_app folder
-- Jinja2
| -- my_app
| -- .html files
| -- static
-- my_app
-- styles
-- .css file
That is, jinja2 and static are both folders on the same level inside my_app folder.
My base.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!--bootstrap responsive viewport: mobile device optimization first -->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<bootstrapp css goes here>
<link rel="stylesheet" type="text/css" href="/my_app/static/styles/my_app.css">
<!-- defines an area that child templates fill -->
{% block head %}
{% endblock head %}
</head>
I tried using the {% static 'path/goes/here.css' %} and got an error message on 'static' unrecognized. But if I use the normal {% load static %} Django tags at the top of the file, I get an error that 'load' is an unrecognized tag (which I expected because I'm using Jinja2).
Bootstrap css loads. I've tried excluding the bootstrap links, that makes no difference. I've tried using a static prefix in Jinja2 environment globals, as explained here, it seems to make no difference.
I've read the Jinja2 and Django documentation and followed all the set up recommendations. css templates load in other Django projects where I'm not using Jinja2, using {% load static %}.
Is hardcoding the href the right thing to do? I've tried every possible combination of path reference in case I was just putting the wrong path there. The error message is 'not found: path/etc/etc.css'
Any help much appreciated, thanks!