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
Related
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 have a frontend react app, after using npm run build it creates build folder with:
build
favicon.ico
index.html
service-woker.js
static
After using django's python manage.py collectstatic I noticed what django has done was that it pulls out only the static folder, favicon.ico is not pulled out. So, my website icon doesn't work.
In my index.html, <link rel="apple-touch-icon" href="%PUBLIC_URL%/favicon.ico" />
In my settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../frontend/build/static')
]
STATIC_ROOT = '/var/www/web/home/static/'
STATIC_URL = 'home/static/'
In chrome inspect in the headers element:
<link rel="icon" href="./home/favicon.ico">
How do I get it to display my web icon. Thankyou!
It is clear in documentation that Django collectstatic looks only for files in folders that are set in
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '../frontend/build/static')
]
This will copy all files from your static folders into the STATIC_ROOT
directory.
your favicon is not in any of listed staticfiles directiories
Second thing is that Django static files are only accessible from full STATIC_URL path ( you cannot use just .home/ path)
Fix would be one of following
to simply add icon inside static folder
use ngnix to serve static files and add proper blocks ( prefered )
change STATIC_ROOT='/var/www/web/home/' and STATIC_URL = 'home/' ( note this way index.html and rest of files in home would be accessible as staticfiles)
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 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>
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.