staticfiles loaded the wrong path - app_name/static/ - django

it's my very beginning with the django. I've configured server with nginx and gunicorn. The problem is that static files are not being loaded correctly.
When I go to the source code I can see, f.e:
<link href="/app_name/static/css/bootstrap.min.css" rel="stylesheet">
although the correct file is located under: /static/css/bootstrap.min.css
So it seems that "app_name" is added before path to my /static/ folder.
settings.py file:
STATIC_ROOT = '/webapps/filmyposlowie/static/'
STATIC_URL = '/static/'
index.html file:
{% load staticfiles %}
<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet">
nginx:
location /static/ {
alias /webapps/filmyposlowie/static/;
}

Restarting the server after changing setting.py file has helped me. In my case it was: supervisorctl restart [process_name]

Do you have a STATICFILES_STORAGE setting in settings.py?
If not, try changing {% load staticfiles %} to {% load static %} in your index.html file. I had a similar problem once.

Related

How to connect the css?

How can I plug css into html by importing css from the main folder? I have a network project with 3 app.
network
--chat
--news
--static/style.css
...
My code:
{% load static %}
<link rel="stylesheet" href="{% style.css' %}">
It works if I create a static folder in some app, but I want to put it in the main network folder and then it stops working.
In your settings.py;
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Then inside your html files;
{% load static %}
<link rel='stylesheet' href="{% static 'styles.css' %}">
Explanation
first of all i am setting the STATIC_URL which helps in the way that everytime you don't need to provide the path of the static files but use the syntax as i used ("{% static 'styles.css' %}").
After that i set the STATICFILES_DIRS which sets the directories where the app should search for the static files which i set to the static folder present in the base directory. You can set it as you want.

Django static files not loading in production

I always get 404 error in loading static files in an EC2 instance. I have searched for hours and tried to implement various suggestions but nothing worked.
Following is my configuration of different files:
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
LOGIN_REDIRECT_URL = '/'
html
{% extends "base.html" %}
{% load staticfiles %}
{% block extra_head %}
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.js"></script>
<script src="{% static "ddv_example/ddv_example_1_10.js" %}"></script>
<script type="text/javascript">
nginx.conf
location /static/admin {
alias /usr/local/virtualenvs/mysite/lib/python2.7/site-packages/django/contrib/admin/static/admin;
}
location /static/rest_framework {
alias /usr/local/virtualenvs/mysite/lib/python2.7/site-packages/rest_framework/static/rest_framework;
}
location /static {
alias /usr/local/apps/mysite/src/mysite/mysite/static;
}
location /static/ddv_example {
alias /usr/local/apps/mysite/src/mysite/mysite/static/ddv_example;
}
Any suggestions on what am i missing out ?
I had missed out re-running sudo ./server_setup.sh. The nginx configs will take effect only when the script is re-run.

Django Static wont Load

I am learning how to use django but having difficulties using static files.
Files (BASE_DIR is website, App is player):
Website
└─── player
└─── static
└─── style.css
└─── admin
In Settings:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'player/static')
In index.html:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{ static 'style.css' %}" >
In style.css:
body {
background-color: blue
}
I have ran collectstatic.
When loaded, the HTML appears without the css.
The console reads:
[23/Nov/2016 23:33:13] "GET / HTTP/1.1" 200 278
Not Found: /{ static 'style.css' %}
[23/Nov/2016 23:33:13] "GET /%7B%20static%20'style.css'%20%%7D HTTP/1.1" 404 2172
Did you run the python manage.py collectstatic?
Wrong directive in template:
{% load static %}
should be
{% load staticfiles %}
Update
also not:
{ static 'style.css' %}
but
{% static 'style.css' %}
Basic spelling errors

Django: Wish to have my css file located in my project instead of an app

I want to save my style.css file in the main project folder (for my project in django that is) under either /static/ or /templates/ but I cannot seem to load it properly. I use:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static '/style.css' %}" \>
This css file is suppose to be loaded with my /templates/base.html site (stored in the main project folder).
Thanks,
Matt
Loading of static files from templates directory is a bad idea - source of your templates will be available to web user.
To load files from static/ directory add the STATICFILES_DIRS to your settings.py:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
And BTW remove the first slash in the {% static %} tag:
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}" \>

Loading static files in template

I am pretty new to Django and I am trying to put static files in my site but I am unable to do so. I am getting 404 when the browser tries to GET my static files
I'm not sure of the best practices or the correct way for Django to find these static files
Here is a picture of my project structure:
Here is my settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
In my index.html I have a line:
<link href="{{ STATIC_URL }}boothie.css" rel="stylesheet">
<script src="{{ STATIC_URL }}boothie.js"></script>
<script src="{{ STATIC_URL }}jquery.js.1.3.js"></script>
I think this would work:
{% load staticfiles %}
<link href="{% static "css/boothie.css" %}" rel="stylesheet">
<script src="{% static "js/boothie.js" %}"></script>
<script src="{% static "js/jquery.js.1.3.js" %}"></script>
See: Configuring static files
Edit:
You can maintain static files which belong to your home app with the structure like this:
home/
static/
css/
images/
js/
While deploying, you can set STATIC_ROOT to where you want to serve these static files, then run python manage.py collectstatic, django will copy your static files into the STATIC_ROOT directory and you can maintain static files easier (in a single directory) without changing your code.
static_url will give the relative path ....
You may have to include
<link href="{% static "css/boothie.css" %}" rel="stylesheet">