Docker Django can't find static files 404error - django

I would like to deploy my website in django to docker. Problem is, when i run docker-compose up, website html is loaded, but static content like css, js, logo, mp3 not. I think that this is because my url. On localhost my website runs correctly, but server has prefix something like http://127.0.0.1:8000/Something. I repaired urls so html files run but my static files in settings.py looks like this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
and for example my homepage.html with js and css included (this works on localhost) like this:
<head>
<meta charset="UTF-8">
<title>Title</title>
{% load static %}
<link rel="stylesheet" href="{% static "styles/homepage.css" %}">
<script src="{% static "scripts/homepage.js" %}"></script>
</head>
but when server runs on http://127.0.0.1:8000/Something it show up this error in the command line:
As you can see, the path must be not http://86.110.225.19/static/styles/homepage.css but
http://86.110.225.19/SOMETHING/static/styles/homepage.css
How can i fix this? Thank you so much.

Change the configuration to:
STATIC_URL = '/Something/static/'

I find out, what made this error. I added to settings.py line:
SUB_SITE = '/Something/'
and it works! :)

Related

Django 4 Static files : what's the best practice

I'm currently building a project on Django 4.0 and I want to do the static files management the best and the cleaner for this version.
Currently I have this project tree :
And here is my settings file :
BASE_DIR = Path(__file__).resolve().parent.parent.parent
(...)
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
When I try to find some videos about the subject, no one is using the same structure and setup for static files. In this sample, I have a 404 error on my dist/css/output.css file.
In my HTML template I try to call it that way :
<link href='{% static "css/dist/output.css" %}' type="text/css" rel="stylesheet">
Could someone please copy/past me an easy static setup for handling static properly ?
Or at least, help me to understand why it doesn't work and what I should do ?
Moreover, I put my static directory outside my main app, but some are putting it in. So I don't know what's best...
Thanks :)
The best way to configure your static files in django 4 is to use pathlib instead of importing an extra module eg. os
STATICFILES_DIRS = [
BASE_DIR / "static",
]
and problem is that you don't have css folder inside your static folder so instead of this
<link href='{% static "css/dist/output.css" %}' type="text/css" rel="stylesheet">
you have to put this
<link href='{% static "dist/output.css" %}' type="text/css" rel="stylesheet">
or you can create css folder and add dist folder inside it

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 not finding my static files

I'm trying to reorganize my project, as the files in it are very unorganized. There is a static folder in the main project folder that should only have all of the apps. So I'm relocating some of the .js files into their respective apps.
One of the files is in /static/js/mmm and I'm trying to move it to mmm/static/mmm. I copied the file over and changed the code in one of my templates (located in mmm/templates/mmm) from
<script src="/static/js/mmm/filemanage.js" type="text/javascript"></script>
to
{% load staticfiles %}
<script src="{% static "mmm/filemanage.js" %}" type="text/javascript"></script>
However I opened the page and the js console and it is trying to access the file like this:
http://fakedomain.com/static/mmm/filemanage.js
From my understanding it should be looking in
http://fakedomain.com/mmm/static/mmm/filemanage.js
In my settings file I have 'mmm' as an installed app and
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)
STATIC_URL = '/static/'
Not sure what I'm doing wrong here as I don't totally understand how Django searches for the static files. I also didn't put those things in the settings files so I don't understand what they're doing. The Django tutorial, part 6 says that "Django’s STATICFILES_FINDERS setting contains a list of finders that know how to discover static files from various sources. One of the defaults is AppDirectoriesFinder which looks for a “static” subdirectory in each of the INSTALLED_APPS, like the one in polls we just created. "
I believe your problem is with this line:
<script src="{% static "static/parcelManage.js" %}" type="text/javascript"></script>
Reveiw the documentation for static files here.
Basically, it looks like you should change the line to:
<script src="{% static "measuring/parcelManage.js" %}" type="text/javascript"></script>

PyCharm3, how to get rid of "cannot resolve directory" messages?

I am using PyCharm3 for developing and I get "Cannot Resolve Directory" errors in places like these:
<link href="/static/css/styles.css" rel="stylesheet" type="text/css" />
However, if I do this:
<link href="/my_project/static/css/styles.css" rel="stylesheet" type="text/css" />
I do not get the error any more. So I guess it must be something in PyCharm's settings that I can fix. But what is that?
This is my settings.py
import os
PROJECT_DIR = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(PROJECT_DIR, "staticfiles/")
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, "static"),
)
I also tried to set static/ as "sources" folder.
PS: This is not a django error. It is a PyCharm error. My web application is working fine.
I know it has been asked a while ago. I faced the same issue with Intellij with Python Plugin similar to Pycharm.
Fix is as follows.
Mark the template folder as Template source.
Then you can add the template language as django
in language and
frameworks Ctrl+Shift+S
See the following details for reference
Use this
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
as per docs found here: https://docs.djangoproject.com/en/1.6/howto/static-files/
I was having the same problem. What I've done to fix this was:
I've enabled Django support on Pycharm = Settings -> Languages & Framework -> Django