Change django's default static directory - python

I got an issue with Django 1.6:
I want to change the default static file directory in django. I don't want it in
project/myapp/static but in project/static
I readed django's documentation, added
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIR =(os.path.join(BASE_DIR, 'static'),)
In my settings.py
Then I ran ./manage.py collectstatic and files copyed as expected.
Finally I launched the server, the app is using django boilerplate plugin, so my template begins with:
{% extends 'dh5bp/base.html' %}
{% load url from future %}
{% load staticfiles %}
{% block head %}
<link rel="stylesheet" href="{% static "css/homepage.css" %}">
{% endblock %}
And the Css won't load: But in my server log, I got that:
[29/Aug/2014 11:23:03] "GET /static/js/dh5bp/plugins.js HTTP/1.1" 304 0
[29/Aug/2014 11:23:03] "GET /static/css/homepage.css HTTP/1.1" 404 1657
As you see the statics file from dh5bp (Boiler plate plugin) Are loaded correctly, while the statics from my app aren't loaded correctly.
I tried to add + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to my urls.py, right after urlpatterns, It didn't worked.
So please if someone could tell me chat I'm doing bad and what should I change in the settings. It could be great
EDIT:
Did tryed the solution here, It give me the same result: only statics from boilerplate are loaded.
And not to mention that I've obviously checked if the files exists in /project/static, they does exists.
EDIT 2:
I tried to put the old static folder in my app, to ensure That he weren't looking for the files in the old folder. It doesn't, so I don't know where django expect those file to be? Is there a debug setup that could help on this?

Your STATIC_ROOT shouldn't be in STATICFILES_DIRS.
STATICFILES_DIRS should contain paths to your project's static files.
STATIC_ROOT is where all your static files are collected when you run collectstatic.
If you run django server with DEBUG=True, server will serve static files straight form STATICFILES_DIRS, and if DEBUG=False it won't handle static files at all. In that case you can run django server with option --insecure.
See this related question for more.

Related

Getting 'GET /static/css/base.css HTTP/1.1" 404 1795' error for static files

I have no idea what I'm doing wrong. I have STATIC_URL = '/static/' STATICFILES_DIR = [str(BASE_DIR.joinpath('static'))] under my settings.py. Here is an image to my current file structure https://pasteboard.co/K3uhtSN.png I linked with <link rel="stylesheet" href="{% static 'css/base.css' %}"> in base.html and loaded at the very top using {% load static %} Thank you
I had the same problem, but it solved by writing "staticfiles_dirs" like this:
STATICFILES_DIRS = ((os.path.join(BASE_DIR, 'static')), )
I had the same problem. If you had created a 'staticfiles' folder or directory during production then be sure to run python manage.py collectstatic in your command prompt or terminal.

Unable to load static files in Django Production

I am trying to run the app with DEBUG=False
Below is my setting file configuration
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/")]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
After running python manage.py collectstatic, all the static files in the app path are copied to staticfiles directory (mentioned in STATIC_ROOT path).
When loading the webpage, the static files are failed to get load.
Error message:
GET /static/dist/bootstrap-4.0.0-dist/js/bootstrap.min.14d449eb8876.js HTTP/1.1" 404 77
GET /static/dist/bootstrap-select/js/bootstrap-select.min.31f649694651.js HTTP/1.1" 404 77
GET /static/js/base.1332bbb46ac5.js HTTP/1.1" 404 77
GET /static/crsummary/dist/amcharts4/core.ea1ec0eb6727.js HTTP/1.1" 404 77
Looking at the error message, apps is trying to load the bootstrap.min.14d449eb8876.js from path /static/\*/\* but the actual file location is staticfiles/\*/\*
I am not sure what configuration that I have missed out here.
Django doesn't allow to load staticfiles when codes in production, instead of using aws or any other online content delivery network
If you really wan't to load staticfiles from the codes directory , you need to install whitenoise , and define it in your middleware, staticfiles, and installed_apps
for more you can search for use and settings whitenoise in django
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href=" {%static 'app1/css/freelancer.css' %}" />
Load the static files like this.
{% load static %}
<img class="img-fluid" src="{% static 'app1/img/portfolio/ai.png' %}" alt="">
Load the image like this.
Before that , Required to specify the Base directory and static directory
STATIC_DIR=os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS=[STATIC_DIR,]
Base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Build paths inside the project like this: os.path.join(BASE_DIR, ...)

static files arent working in django, but i have the same settings as another project I have done in the past?

DEVELOPMENT SERVER - Can't get my static files to work(CSS), I am not sure what I'm doing wrong but I cant seem to get them to load at all.
my HTML template
{% load static %}
<link type="stylesheet" href="{% static 'main.css' %}" />
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [
"C:/Users/Sam/Documents/Django/Blog/OrapiApplied/static"
]
My file layout
/letsdoit
manage.py
/app
/static
main.css
/letsdoit
settings.py
The setting is STATICFILES_DIRS, with an S.
If you add "django.contrib.staticfiles" to your INSTALLED_APPS setting in settings.py, the Django runserver development server will automatically look for all static directories in all your apps.
If you also have static files in other locations (e.g. if you were to add a static directory inside /letsdoit/letsdoit/ which is not an app), you'd have to add it to STATICFILES_DIRS so that Django also finds those.
Note that it's good pratice to namespace your static files for each app:
/app
/static
/app
image.jpeg
So that way you can add {% static 'app/image.jpeg' %} in your template and for a different app you'd be able to also have a image.jpeg which wouldn't conflict with the one in app: {% static 'other_app/image.jpeg' %}.
Add this lines to settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
and don't forget to clear cached images and data of browser before loading the page. Sometimes the browser just keeps giving the browser CSS from the cache.

Django static files do not load in correctly

I have done everything as always (I think), but for some reason django static files not working perfectly. I tried to find a solution but nothing helped.
This is the site now:
How it should be:
Inside static folder I have css, images, and so on. I have {% load static %} on the top of the html. I do not have any errors in the console.
One line of the html:
<link rel="stylesheet" href="{% static 'css/style.css' %}">
My settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
My folders:
SOLUTION: I have deleted pycache folders and the migrate folder,
then started the server again. It solved the problem. I do not know
the real reason, why...
I have deleted pycache folders and migrate folder. Now everything semms to be fine :) I do not know what was the reason of the problem.

Bootstrap does not be set to Django app

Bootstrap does not be set to my Django app.In google console,Failed to load resource: the server responded with a status of 404 (Not Found) bootflat.min.css error happens.I wrote in settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG =True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = [os.path.join(BASE_DIR,'bootflat.github.io'), ]
in PythonServer_nginx.conf
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name MyServerIPAdress; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /home/ubuntu/PythonServer/PythonServer/accounts/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/PythonServer/uwsgi_params; # the uwsgi_params file you installed
}
}
I do not know why Bootstrap does not be set to my Django app.I run command python manage.py collectstatic but no error happens.How should I fix this?What should I write it?
I assume the project works fine in local, this problem occurs only when you use nginx. If that is the case probably the path you mentioned in the nginx config for location /static is wrong, try getting the absolute path to the static folder from the server and see.
I have no way to test my answer for obvious reason, but your STATICFILES_DIRS and STATIC_ROOT don't match. If you change your STATICFILES_DIRS to [os.path.join(BASE_DIR,"/static/"),'https://bootflat.github.io'], it might work. I suggested so because it doesn't make sense to me to os.path.join a local path with a public URL.
Try this,
In settings.py:
STATIC_DIR = os.path.join(BASE_DIR,"static")
Now at last, add these lines:
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
Now, put all your bootstrap files in a 'static' folder under your project folder.
At your HTML code,
You have to load the static files using:
{% load staticfiles %}
Now,you can use your static files using:
<link rel="stylesheet" href="{% static "assets/css/bootstrap.min.css" %}">
P.s. my 'bootstrap.min.css' is present under 'static' folder and then 'assets' and then 'css' folder.
It works perfectly for me.
First: where are you storing your static bootstrap files? I recommend storing them in the app's directory where you are referencing them in a template. For example, if you have an app myapp I would put them in /myapp/static/myapp/. So if I'm storing bootstrap.css it would go in /myapp/static/myapp/bootstrap.css.
Next, in your template you need to reference the correct static folder. For the Bootstrap stylesheet, the href would look like href = "{% static 'myapp/bootstrap.css' }%"
Lastly, in your settings.py file add the line STATIC_ROOT = '/home/ubuntu/PythonServer/PythonServer/accounts/static' (based on what you wrote, just make sure that it's pointing to the correct deployment folder where you're keeping your live static files) and run collectstatic again.
First of all you do not need STATICFILES_DIRS since django will collect static files for you from each INSTALLED_APP specified in your settings.py. You need to add bootstrap4 to INSTALLED_APP:
INSTALLED_APPS = [
...
'my_great_app',
'bootstrap4',
]
You need to specify STATIC_ROOT and STATIC_URL as you do in your settings.py. You need to understand why you use each setting.
Django uses STATIC_ROOT in collect_static command which collects
all static files from apps and directories in STATICFILES_DIRS puts
in STATIC_ROOT. For example you created a new app which has it's
required static files in itself, you run ./manage.py collect_static
to put your static files in STATIC_ROOT in which web server (nginx
in your case) serves them. At your installation STATIC_ROOT should be
showing to /home/ubuntu/PythonServer/PythonServer/accounts/static
as seen in your nginx config.
STATIC_URL is used to create static file urls when you use
static template tag. Since the files are served by nginx you need a
specific path, host etc.
STATICFILES_DIRS is used to add static files which are not residing
in apps folders. I do no recommend its use for your case.
On your template you need to use something like the following:
{% extends 'bootstrap4/bootstrap4.html' %}
{% load bootstrap4 %}
{# Display a form #}
<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
Taken from django-bootstrap4 github repo.
By extending bootstrap4/bootstrap4.html you would get required css and js files in your html. With {% load bootstrap4 %} you can use bootstrap4 templatetags and filters.
Try by adding following code in your url.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}
))
Add static root variable in settings.py as shown by Luke B
STATIC_ROOT = '/home/ubuntu/PythonServer/PythonServer/accounts/static'
Check here documentation https://docs.djangoproject.com/en/2.0/howto/static-files/

Categories