163đź‘Ť
No, it is not impossible. Try including {% load staticfiles%}
in the same html file, rather than attempting to inherit it from some base.html
.
20đź‘Ť
Just add {% load static %}
to the top of your template after the {% extends 'app/base.html' %}
.
- [Django]-Charts in django Web Applications
- [Django]-Modulus % in Django template
- [Django]-Django: TemplateDoesNotExist (rest_framework/api.html)
12đź‘Ť
1.) in settings.py add A TUPLE :
STATIFILES_DIR = (
os.path.join(BASE_DIR,’assets’) ,
)
2.) in urls.py add :
from django.contrib.staticfiles.urls import staticfiles.urlpatterns
urlpatterns += staticfile_urlpatterns()
3.) in the html file where you are putting the “link rel=’stylesheet’ ..” , just add at the top :
{% load static from staticfiles %}
and then use :
<link rel="stylesheet" href="{% static 'assets/css' %}"
- [Django]-Django urlsafe base64 decoding with decryption
- [Django]-Get count of related model efficiently in Django
- [Django]-Django – filtering on foreign key properties
3đź‘Ť
My solution is to include
another page with {% load static %}
and script with static reference. {% block xxx %}
expects the first {% yyy %}
not to be other than {% include %}
and {% endblock %}
(the only cases I have observed); so when we use "{% static 'xxx.js' %}"
it breaks and complains. But including another page will put Django in calm.
For example, I have a page homepage
which extends base.html
and has some static js files which are not included in base.html
.
base.html
{% block page %}
{% endblock %}
{% block script %}
{% endblock %}
homepage.html
:
{% extends 'base.html' %}
{% block page %}
...
{% endblock %}
{% block script %}
{% include 'home_js.html'%} <!-- don't use static links here because Django does not like it. -->
{% endblock %}
home_js.html
:
{% load static %}
<script src="{% static 'scripts/jquery.js' %}" ></script>
<script>
function ...
</script>
Now the scripts loads.
So, in a block we cannot use {% %}
tags other than {% block xxx %}
and {% endblock %}
.
I am using Django 5.1.
EDIT:
I found {% verbatim %}
tag to be our savior under such situation.
- [Django]-How to write setup.py to include a Git repository as a dependency
- [Django]-How to express a One-To-Many relationship in Django?
- [Django]-How to add data into ManyToMany field?
-1đź‘Ť
If you are you are using Apache, make sure you have configured the virtual host to serve static files, for example in 000-default.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin webmaster@localhost
Alias /static /home/Dev/cfehome/src/static
<Directory /home/Dev/cfehome/src/static>
Require all granted
</Directory>
<Directory /home/Dev/cfehome/src/cfehome>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess cfehome python-path=/home/Dev/cfehome/src:/home/Dev/cfehome/lib/python3.7/site-packages
WSGIProcessGroup cfehome
WSGIScriptAlias / /home/Dev/cfehome/src/cfehome/wsgi.py
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-How to annotate Count with a condition in a Django queryset
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
-2đź‘Ť
Yes. Django won’t allow it.
You can just use the appropriate path like:
<link rel="stylesheet" href="/static/shop/style.css" />
But be aware: If you change your app’s STATIC_URL
, the href
above must also be updated accordingly.
From Configuring static files:
In your templates, either hardcode the url like /static/my_app/example.jpg or, preferably, use the static template tag…
- [Django]-What is the right way to validate if an object exists in a django view without returning 404?
- [Django]-Where to store secret keys DJANGO
- [Django]-Django TypeError: 'RelatedManager' object is not iterable