Django Static Root 404 Error - python

Right now I am trying to connect my django project html files to static files but keep getting 404 errors. My Project is organized as
src
admin
homePage
media
static
homepage
css
images
fonts
js
templates
in my settings file i have
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myProjectName/static'),
)
and in my html file
{% static "home/css/bootstrap.css"%}
Why do I keep getting 404 errors ? Thank you for your time.

Turn on Debug=True in your settings file and the error page will help you to find out the issue.

Related

Django static images receive 404 error in windows production

I am trying to deploy a Django app on a Windows server. I am able to make the pages load and am using wgsi. I am also able to load pages with images when using runserver, just not when accessing via the webserver. I have DEBUG = False.
My settings.py looks like this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = "/assets/"
When I do collectstatic, my files are copied into the assets folder.
But, when served, I receive:
GET http://localhost:8000/static/js/main.js net::ERR_ABORTED 404 (Not Found)
Try this in your urls.py:
urlpatterns = [
.....
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT )
also define the media root and url in you settings.py:
MEDIA_ROOT = 'media'
MEDIA_URL = '/media/'
for more details: https://docs.djangoproject.com/en/2.1/howto/static-files/#serving-files-uploaded-by-a-user-during-development

Unable to connect background Image with static file Django

I have following html template
<div class="owl-slide cover" style="background-image: url({% static 'img/slides/slide_home_2.jpg'%});">
</div>
But I am not able to load static background.
my static directory is same as it is in django project path along with templates.
here is my part of setting.py for static files
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/',)
Everything seems look ok but my background is not working any suggestion will be helpful
Sorry to hear that. I don't know if you have solved you issue meanhwile.
In my settings.py I have the following STATIC setting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Within every Django-App in my Django-Website I have a directory like this
<app-name>/static/<app-name>/...
And within the templates I am using the following code:
At the very beginning
{% load static %}
What I also recognized in your code is that your path starts with "img/"
If I want to refer to an image then I use
{% static "<app-name>/image.png" %}
at least when deploy on a server I have to execute the following command so Django collects all static files and make it available.
python manage.py collectstatic
Maybe this helps?

Django Localhost not loading static files

I have a working app and have downloaded the relevant django files locally (via git) and am trying to run the app locally. However, static files are not loading.
I receive the following console error when accessing, say, the home page (http://localhost:8000/home/) of the app:
GET http://localhost:8000/static/imported_JS/jquery/jquery.min.js net::ERR_ABORTED
or this error:
http://localhost:8000/static/globe.png 404 (NOT FOUND)
In my settings.py file, I can confirm:
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)),'..')) # i.e. location of settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
I can also confirm there is indeed a static_cdn/ file directory and it does contain the relevant static files.
The live dev and prod versions of the app work (which are hosted on separate web servers) but, for whatever reason, when running the app locally, static files are not served.
Any ideas on how to have the localhost server static files? Most of the answers on SO regarding this type of question relate to mistakes with setting up STATIC_URL vs. STATIC_ROOT but these appear to be correct (at least on dev and prod).
Let me know if you require more info. Thank you.
UPDATE
Well, I struggled with this problem for an hour or so, and 5mins after posting this SO question, I think I found a solution.
Changing from this:
STATICFILES_DIRS = []
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
to this made the difference:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static_cdn/') # add STATIC_ROOT to DIRS
]
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn/')
I'm not sure why this works but I'll indicate your response as Answer if you explain why. Thanks.

How to add my own files to django 'static' folder

I've read django static files document and made my django static files settings like this
setting.py
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
html page
<img src="{% static "admin/img/author.jpg" %}" alt="My image"/>
So if I address one of django default static files, it works fine. But if I add my own file and folders to the static folder, it doesn't show it.
I tried
python manage.py collectstatic
But nothing changed. How can I make it work?
A few things...
STATICFILES_DIRS = (
'path/to/files/in/development',
)
STATIC_ROOT = 'path/where/static/files/are/collected/in/production'
When DEBUG = True, Django will automatically serve files located in any directories within STATICFILES_DIRS when you use the {% static 'path/to/file' %} template tag.
When DEBUG = False, Django will not serve any files automatically, and you are expected to serve those files using Apache, Nginx, etc, from the location specified in STATIC_ROOT.
When you run $ manage.py collectstatic, Django will copy any and all files located in STATICFILES_DIRS and also files within any directory named 'static' in 3rd party apps, into the location specified by STATIC_ROOT.
I typically structure my project root as such:
my_project/
/static_assets/ (specified in STATICFILES_DIRS)
/js
/images
/static (specified in STATIC_ROOT)
I hope that helps you understand how the staticfiles app works.
STATIC_ROOT is where files are collected and moved to when collectstatic runs.
If you want files to be consolidated to there they should be found by the static file finder.
As an example, include the following in your settings
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder")
now for one of the apps that are a part of your project, create a folder called static and put something in it.
When you run collectstatic you should see that file mentioned and it copied to your STATIC_ROOT
You can try adding
STATICFILES_DIRS = (
STATIC_PATH,
)
to your settings.py.
Also, if you haven't already, make sure to include
{% load static from staticfiles %}
in each of your templates where you wish to reference static files.
Lastly, make sure that the file you are referencing actually exists and the file path to it is correct.
Try to:
<img src="{{ STATIC_URL }}admin/img/author.jpg" alt="My image"/>
And in your view must be something like this:
...
from django.shortcuts import render_to_response
from django.conf import settings
def my_view(request):
...
static_url = getattr(settings, 'STATIC_URL')
...
return render_to_response(
template_name,
RequestContext(request, {
'STATIC_URL': static_url,
}))

Django 1.4 serving media files works, but not static

I am using Django 1.4 and for some reason i am able to serve media files, but not the static ones...
Here is my code:
settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
urls.py:
(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
base.html:
<link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet">
<link href="{{ MEDIA_URL }}css/bootstrap-colorpicker.css" rel="stylesheet">
i get a 404 http not found... what am i doing wrong? An i did create the static folder in my project right next to media
http://mysite.com:8000/static/css/bootstrap.css
Your static folder should be under one app that you use it for.
For example, I have a project named my_project and an application named my_app, I have some static files used in my_app so I put them under ~/project_path/my_project/my_app/static
NB: my_app must be in INSTALLED_APPS. See STATICFILES_FINDERS documentation.
Edit:
As a best practice, you should have a global static folder in one app (the main one), for example a static folder how contains your html template basic resources as jquery, bootstrap, your global style.
And for the static files how's required only for one app, for example app foo, these files should be under foo/static folder
I suggest removing the explicit media and static views and allowing the staticfiles app to create them (when DEBUG is True under development).
Check the default finders are present in your settings.py
https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_FINDERS
Either add your project static directory to STATICFILES_DIRS (https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS) or place app specific static folders under each app. The app needs to be listed in the INSTALLED_APPS for the finders to locate the static content.
Do not place static files into STATIC_ROOT yourself. This directory is managed by the collectstatic command. See https://docs.djangoproject.com/en/dev/howto/static-files/#deployment
Here is how i define my media url in settings.py:
import os
ABSOLUTE_PATH = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(ABSOLUTE_PATH, '../media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(ABSOLUTE_PATH, '../static')
STATIC_URL = "/static/"
So like you see the difference is ../media and ../static
Is mysite in your installed apps ? Django look inside your installed apps and check if there's a static folder there.

Categories