Django static directory name is set to static
by default. Can it be changed to something else?. If i could change it to cssandjs
could this bring any errors to my project? and does that also mean that in templates i would load static by {% load cssandjs %}
instead of {% load static %}
?
Asked
Active
Viewed 394 times
0

zeuskedev 1
- 3
- 1
-
Does this answer your question? [django html template can't find static css and js files](https://stackoverflow.com/questions/66437690/django-html-template-cant-find-static-css-and-js-files) – Ivan Starostin Mar 25 '21 at 15:50
-
no it doesn't. My question was can i change the ```static``` directory name to something else so in templates it won't get presented as ```/static/css.css```. ```/static/``` comes to the path when you use the ``` {% static 'css.css' %}``` – zeuskedev 1 Mar 25 '21 at 15:59
2 Answers
1
the statics files con also be loded from more than one directory, this can be defined by this setting, this is where django search your files inside your server
STATICFILES_DIRS = [
BASE_DIR / "static",
'/var/www/static/',
'/some_folder/cssandjs',
]
thats why you put the files inside apps folder, to avoid overlapping
the url is defined by this setting, this is what you see on the web browser
STATIC_URL = '/static/'
in your template you always use {% load static %}, and django search in all your folders

Guillermo Silva
- 371
- 2
- 7
-
so all django projects templates will have a ```href="/static/css/this.css``` in the place of linking the stylesheets? – zeuskedev 1 Mar 25 '21 at 15:33
-
will have {% load static %} at the top and {% static 'css/this.css' %} – Guillermo Silva Mar 25 '21 at 15:53
-
the static url can be diferent of your static folder, you can have many static folders with diferent names – Guillermo Silva Mar 25 '21 at 15:55
-
when you do ```{% static 'css/this.css' %}``` this comes out as ```/static/css/this.css``` wanted to know if i can get rid or change the name of this ```/static/``` – zeuskedev 1 Mar 25 '21 at 15:57
-
you can, but this is related to your web server (apache, nginx, etc) – Guillermo Silva Mar 25 '21 at 16:50
-
maybe you can try whitenoise, and define a folder with blank prefix http://whitenoise.evans.io/en/stable/ – Guillermo Silva Mar 25 '21 at 16:51
0
You can't change {% load static %}
to {% load cssandjs %}
because cssandjs is not a registered tag library. Instead, you can change the directory name (for example static
to cssandjs
) and the same for static_url='/static/'
to '/cssandjs/'
.

double-beep
- 5,031
- 17
- 33
- 41