Django not finding my static files - django

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>

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

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.

Docker Django can't find static files 404error

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! :)

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

static files in pybb

I have some problems with static file, when i use them in pybb. I work on django 1.3 and python 2.6.5.
My settings file is here
I put my css and images in /static/pybb/css and /static/pybb/images
when i put tag like
<link rel="stylesheet" href="{{ STATIC_URL }}pybb/my.css">
in my templates, it doesn't work. This is the same with image
<img src="{{ STATIC_URL }}pybb/images/myimage.jpg">
To sum it up, my own static files don't appear in my project templates.
any idea? perhaps i forgot something
Why are you putting css in package static files directory?
Also directories in STATICFILES_DIRS should not overlaps. Look at example provided with pybbm or even start project from this example.