Django static files not loading for all pages except index page - django

I am working on a blog website in django, where I can write articles.
While developing I am simultaneously hosting it on pythonanywhere.com
In dev server everything works perfect, but after deploying, I am facing a strange issue. My static files are loading for home page but are giving error-404 for all remaining pages.
While debugging, I found that for home page it searches files in path www.asdf.pythonaywhere.com/static which is correct
But for a page say 'www.asdf.pythonaywhere.com/article/1/' it searches for static files in ''www.asdf.pythonaywhere.com/article/1/static' which is incorrect.
I searched everywhere on the internet but couldn't find answer.
Here are my settings:
STATIC_URL = 'static/'
STATIC_ROOT = 'static/'
This is how I am referring to static files:
<link href="{% static "vendor/bootstrap/css/bootstrap.min.css" %}" rel="stylesheet">

I guess the problem is that you are using relative addressing for your static_url and root. change it to this:
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
add '/' before your static urls

Related

Django : Page Not Found when trying to access static files

I know there are already several threads on the topic. I've been through most of them (especially all the troubleshooting listed in this one) but I can't figure out my issue.
I am trying to use a Bootstrap template in my Django project, and I'd like to simply start by accessing the files in the /static/ directory. My project directory looks like this :
Whenever I try to load the page http://localhost:8000/static/theme/assets/css/style.css it returns a Page not found error (and obviously no CSS/JS content appears on my index).
Here are my settings:
I have debug = True
ÌNSTALLED_APPS contains django.contrib.staticfiles
settings.py looks like this :
STATIC_URL = "/static/"
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),)
But I still can't access anything from the /static/ directory.
I tried to access the CSS and JS files in base.html this way :
{% load static %}
...
<link href="{% static 'theme/assets/css/style.css' %}" rel="stylesheet">
I really have no clue how I could solve this.
Thanks in advance for your help !
Is base.html properly bound to a URL and to a view function via a URL dispatcher ? Can you access that file from your browser ?
If yes, try to substitute this line
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),)
with this one
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

Static files not loaded into templates

I am new to Django and so far all I know about static files is that they are CSS, JS, and images and they should be in a directory called static within the app directory
but when I use one of these files in my template like that:
first I load the static files
{% load static %} <!-- in the 1st line of the template -->
then I link the CSS file like that
<link href="{% static 'auctions/styles.css' %}" rel="stylesheet">
They don't load and the styles don't seem to appear
so I just want to know what I am missing here
this is the project tree enter image description here
static root and url from settings
STATIC_URL = '/static/'
STATIC_ROOT = 'E:/Work/SoftwareDevelopment/Web/Django/commerce/auctions/static'
Did you ran python manage.py collectstatic?
You also need to configurate your settings.py with STATIC_URL = '/static/'
Docs
Are your auctions's static folder separated in css, js, images folders? If so, you are missing specifying that in static:
'auctions/css/styles.css'
If that doesn't work, try running collectstatic manually and getting the path to the file from there to pass in the html.

django-photologue not loading CSS

I have setup django-photologue, and I am trying to get it to load the default templates. I can get the HTML file loaded, but it won't load any of the CSS (mainly just bootstrap)
When accessing photologue, I get the following error on the console:
Not Found: /photologue/gallery/css/bootstrap.min.css
[24/Aug/2018 13:42:52] "GET /photologue/gallery/css/bootstrap.min.css HTTP/1.1" 404 7311
This is odd to me, because I am almost certain that the css file is present.
This is the django code including the CSS file (taken from the photologue example project):
<link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet" media="screen">
The resultant HTML is:
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
No matter where I put the CSS file, I get a 404 error.
I have photologue templates in myapp/templates/photologue, because for whatever reason that's what worked.
The HTML I have there works fine, but the CSS just won't load. Not in it's own subfolder in the photologue templates directory, not as a subfolder in the myapp tempaltes directory, not as standalone files, not when I put them in the static folder....I've tried putting them in every possible location and reloading the site, and it makes no difference.
What can I do to make my template, which loads correctly, load and see CSS files?
edit: settings.py : https://pastebin.com/kRj21j3m
The issue here is that your server is looking for your css file here:
/photologue/gallery/css/bootstrap.min.css
Where it doesn't actually exist, hence the 404 error (file/page not found).
If you're using the base Django instructions for setting up a project, it is highly likely that your static files are found here:
/static/css/bootstrap.min.css.
Please make sure you have the following in your settings.py file:
STATIC_ROOT = os.path.join(BASE_DIR, 'public', 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I also suspect that you may want this in your base.html file (or whichever template you're extending from):
{% load static %}
And then this in your template's head:
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet" media="screen">

Static File Whitenoise Heroku Django issue

I have an API developed in Django and Django Rest Framework. We need one page in "normal" Django that will be open once a month maybe (so no need for CDN for the static files). Gunicorn + whitenoise is what we went ahead with.
The collectstatic works fine in both build phase and after build phase.
The url generated on the page is href=/static/css/edit_card.a1c6e0f9f12e.css/ but the console shows the 404 not found for that resource and there are no styles applied to the page.
Relevant django settings:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_media/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR + "/static_folder/"),
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
The relevant file in the repo is in /static_folder/css/edit_card.css
The relevant file on the heroku instance after running collectstatic is in
static_media/css/edit_card.a1c6e0f9f12e.css (together with the normal version and other compressed files)
I can manually access this link url/static/css/edit_card.css which is ridiculously weird.
This works fine when DEBUG = True. When in False/production it does not.
Could someone point me in the right direction? Thanks.
EDIT:
Template
{% load static %}
<link rel="stylesheet" type="text/css" href={% static "css/edit_card.css" %}/>
It might be too late for this response, but I'm surprised nobody noticed the error.
Your template is as follows:
<link rel="stylesheet" type="text/css" href={% static "css/edit_card.css" %}/>
The problem is that the href attribute is not quoted and it's taking the last / as part of the path. That's why the URL is: href=/static/css/edit_card.a1c6e0f9f12e.css/ (note the / at the end).
The solution would be:
<link rel="stylesheet" type="text/css" href="{% static "css/edit_card.css" %}" />
URL is between quotes and there's a space after the path.

Django template does not render CSS and Javascript

I have the following folder structure for my project.
-App1
-App2
-App3
-App4
-static
-css
-bootstrap.css
-js
-bootstrap.js
-jquery.js
-tempaltes
-base.html
Now, in my base.html file i have
But, when i view the file the css and javascript does not seem to be loaded on the page.
In my, i have
settings.py
TEMPLATE_DIRS = "Absolute-path-to-base.html"
STATIC_URL = '/static'
STATICFILES_DIRS = 'Absolute-path-to-the above static folder'
As per all the docs and posts what i understood was, we need to keep all the staticfiles in one place, viz, static folder in my case...and all the templates (including base.html) in one place. After doing so, i open the base.html in my browser to view the page...and it does not display the CSS and the javascript. Instead when i place the file (base.html) in the static folder things work fine.
Can someone point me in the right direction?
You should replace
STATIC_URL = '/static'
by :
STATIC_URL = '/static/'
and in django templates, you should use
<script src="{% static "path" %}"></script>
also add {% load staticfiles %} at the top