I can not see my static files when running the project on the server (at my desktop it was ok).
When I look for the picture path on my browser I have
/static/app_name/images/image_name
My images are stored at
www.mydomain.com/xxx/xxx/static/app_name/images/image_name
I tried to adjust the settings.py from
'/static/'
to
'/xxx/xxx/static/'
But it seems to have no effect as the images path on my browser are still
/static/app_name/images/image_name and not /xxx/xxx/static/app_name/images/image_name
Am I missing something here?
Thanks for any help!
Changing STATIC_URL = '/static/' is only going to change the URL, not where the images are actually served.
Make sure that STATIC_ROOT is pointing to /path/to/www.mydomain.com/xxx/xxx/static/ and make sure you are using hard-coded paths in your settings.py, not something like
os.path.join(os.path.dirname(__file__, 'static'))
Then in your templates
<!-- either -->
<img src="{{ STATIC_URL }}img/my_image.png" />
<!-- or -->
{% load static %}
<img src="{% static 'img/my_image.png' %}" />
Also, be sure you're running python manage.py collectstatic to collect all of the static files from all of your apps to place them in the STATIC_ROOT directory.
Also
Depending on which server you're using, make sure you have a path alias that points to your static directory. For example, in Apache in your /sites-available/www.mydomain.com conf make sure that this Alias directive exists
<VirtualHost *:80>
...
Alias /static /path/to/www.mydomain.com/xxx/xxx/static/
...
</VirtualHost>
Related
I use whitenoise for static files and it works fine.
But how can I serve the /favicon.ico file?
There is a setting called WHITENOISE_ROOT, but I don't understand how to use it.
I would like to keep my nginx config simple and serve all files via gunicorn
If you want those files to be managed by collectstatic
Let's assume after running collectstatic, your favicon.ico file ends up being copied in a root subdirectory, located in your STATIC_ROOT directory.
Then, with:
WHITENOISE_ROOT = os.path.join(STATIC_ROOT, 'root')
Whitenoise will serve all files in STATIC_ROOT/root/ at the root of your application.
In your case, STATIC_ROOT/root/favicon.ico will be served at /favicon.ico.
If you don't want those files to be managed by collectstatic
You can have a root_staticfiles folder in your BASE_DIR which simply contains the static files you want to serve at /.
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'root_staticfiles')
In this case, Whitenoise will serve all files in BASE_DIR/root_staticfiles/ at the root of your application.
Update about pathlib (2022-10-04)
Since a while now, the default settings.py Django creates uses pathlib. To be consistent with it, one may replace os.join calls with / operators, eg.:
WHITENOISE_ROOT = STATIC_ROOT / 'root'
You could as per this answer by hanleyhansen add the following line in a base template (used by all further templates):
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
Or you could write a redirect view like this answer by wim with some little modification:
from django.views.generic.base import RedirectView
from django.conf.urls.static import static
re_path(r'^favicon\.ico$', RedirectView.as_view(url=static('favicon.ico'), permanent=True))
I have a django app that uses Whitenoise (hosted on Heroku) and serves my favicon from a separate folder from my static files.
Make a folder root_files at path BASE_DIR/root_files.
In settings.py:
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'root_files')
For a real-life code example checkout Mozilla's Bedrock repo. They have favicons in BASE/root_files and configure WHITENOISE_ROOT in settings.py
So, in my index page located in root/templates/home.html I have the following line for loading CSS:
<link rel="stylesheet" href="{% static 'project/home.css' %}">
home.css is located at: root/static/project/home.css
In settings.py:
STATIC_ROOT = "static/"
STATIC_URL = '/static/'
And when I run the server, in the main page CSS fails to load raising 404 in the browser although the browser displaying the correct path where the home.css is located:
http://127.0.0.1:8000/static/project/home.css
For all apps in the projects everything works fine.
When global static is defined in settings.py:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
the problem is solved, but then, for obvious reasons I cannot perform collectstatic unless I rename it to "assets" for example.
I am clearly missing something here but I want to know why it fails to load the home.css from the legit path?
I am attempting to deploy a Django 1.6 application on OpenShift using the Python 3.3 cartridge, but I have run into problems with static files. I have had partial success with the OpenShift IRC channel, tutorials/templates (for example), and previous StackExchange questions (for example), but nothing has completely resolved the problem.
When I request the static content by URL (e.g. 'mydomain.com/static/stylesheet.css' or 'mydomain.com/static/icons/cog.svg') I can see them perfectly fine. When static files are used as SVG data for icons, they show up fine. Only when linking to a stylesheet have I run into problems. I use the following to include CSS in my template:
<link type="text/css" rel="stylesheet" href={% static "stylesheet.css" %}/>
I have loaded the static files tag set with {% load staticfiles %}. Instead of seeing the stylesheet at /static/stylesheet.css, Django (I assume that it is Django, not Apache) looks for it at /static/stylesheet.css/ (note the trailing slash). This causes the request to fail with a 404 status code. The same thing occurs when I use other file extensions (I have tried .txt, .css, and .svg) or link to a file contained in a subdirectory of static. It is only in this circumstance that an extra trailing slash is appended.
It is my understanding that Django appends a trailing slash to a URL in the event that the URL does not match any of the patterns defined in urls.py. Is it possible on OpenShift to configure Apache so that it directly handles all requests to URLs of the form /static/*? I have an .htaccess file in the wsgi directory with the commands
Rewrite Engine On
Rewrite Rule ^application/static/(.+)$ /static/$1 [L]
but this does not solve the problem. I have also tried using a rewrite rule for just the stylesheet as well as a few things with Alias but have had no luck there, either.
Should Django be getting the requests for these static files at all? I have confirmed that DEBUG is being set to False in my settings.py file, and make no mention of django.views.static.serve in my urls.py file. Here are the relevant parts of settings.py:
STATIC_URL = '/static/'
if 'OPENSHIFT_REPO_DIR' in os.environ:
STATIC_ROOT = os.path.join(os.environ.get('OPENSHIFT_REPO_DIR'),
'wsgi', 'static')
else:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
I do not set values for STATICFILES_DIRS or STATICFILES_FINDERS because at present I'm only dealing with static files found at STATIC_ROOT. The OpenShift project looks like
~/app-root/runtime/repo/wsgi/
.htaccess
application
openshift/
settings.py
manage.py
#And so on.
static/
stylesheet.css
icons/
cog.svg
#More icons here.
This is my first time trying to deploy and I am stuck on this stumbling block. Does anyone know what I am doing wrong?
Instead of href={% static "stylesheet.css" %}, try href="{% static 'stylesheet.css' %}"
I'm using a webfaction-hosted django site, and I'm trying to get locally stored images to show properly, but I'm having trouble getting the static directories/permissions/etc. to line up.
I've configured everything according to webfaction's guide at https://docs.webfaction.com/software/django/config.html
In my project's templates folder, I have the line:
<img src="{{ STATIC_URL }}{{ project.overview_image }}">
where project.overview_image is a CharField where I can list a subdirectory where the image is located.
In my settings.py, I have:
STATIC_URL = 'http://morphogen.cc/static/'
STATIC_ROOT = '/home/scottnla/webapps/static_media/'
STATICFILES_DIRS = (
'/home/scottnla/webapps/portfolio_website/portfolio_website/static/'
)
where /webapps/static_media/ is my static media app.
inside of my project's local /static/ folder, i have an image in the folder
/home/scottnla/webapps/portfolio_website/portfolio_website/static/images/small/img.jpg
and when i run 'manage.py collectstatic', the image is copied to:
/home/scottnla/webapps/static_media/images/small/img.jpg
but when I look at my served HTML page, the image doesn't show.
When I inspect the element, I see that the image source is listed as:
<img src="http://morphogen.cc/static/images/small/img.jpg">
which seems consistent with everything above, but if I go directly to that directory, I get a 403 Forbidden Error.
What's the next step in troubleshooting this?
thanks.
Your settings.py should look like this.
STATIC_ROOT = '/home/USER_NAME/webapps/static_media/'
STATIC_URL = '/static/'
STATIC_DIR is outdated and unnecessary for a single static folder
ALSO, you must have
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
IF you're still having problems check the Django Static Files Documentation
I don't know why I'm getting this error continuously modifying the routes of my settings.py, I have the tree directory of my project as follows:
/dgp
/assets
/css
/js
...
/sales
manage.py
/dgp
settings.py
As you can see, I want to access the assets folder which is one folder up to settings.py, in my settings files I have the follow configuration:
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.abspath(os.path.join(PROJECT_ROOT, '..','static'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.abspath(os.path.join(PROJECT_ROOT,"..","assets")),
)
But always I'm getting a 404 error finding assets files:
"GET /static/assets/css/bootstrap.min.css HTTP/1.1" 404 1688
I don't know why it's concatenating /static with /assets but I prove hard-coding with absolute pahts and neither... I don't know what's wrong... any ideas?
How are you referencing the static files in your templates? It looks like you are including assets/ in that path when you shouldn't be.
For example, if you are using {% static "assets/css/bootstrap.min.css" %} try switching it to {% static "css/bootstrap.min.css" %}. If you are using the {{ STATIC_URL }} variable within the templates to get the path, you would also just drop the assets/ part of the path in the same way.