404 not found assets files in django - django

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.

Related

Django Website does not load static files

I am in the middle of creating a django project and I have templates and static folders within the project directory. I can render and view the html files however it cannot load css files stored in the static folder. I have placed the load static tag in my html file but when I run python manage.py runserver it I get this error
Performing system checks...
Watching for file changes with StatReloader
System check identified some issues:
WARNINGS:
?: (staticfiles.W004) The directory '/static' in the STATICFILES_DIRS setting does not exist.
System check identified 1 issue (0 silenced).
December 08, 2022 - 14:54:53
Django version 4.1.3, using settings 'brighterstrat.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
This is how I have referenced the static files in my html file
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css'%}">
<link rel="stylesheet" href="{% static 'css/style.css'%}">
setting.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, '/static')]
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
How can I make it load the css files
Django statics can be confusing, mostly because the behavior is different between prod and development (i.e DEBUG=True)
To recap the settings:
# Url at which static files are served
# It's the url the browser will fetch to get the static files
# It's prepend to static name by the {% static %} templatetag
STATIC_URL = "static/"
# Directory where static files can be found
# When DEBUG = True, static files will be directly served from there by
# the manage.py runserver command
STATICFILES_DIRS = [BASE_DIR / "static"]
# Directory to export staticfiles for production
# All files from all STATICFILES_DIRS will be copied by
# manage.py collectstatic to this directory.
# /!\ It will not be served by django, you have to setup
# your webserver (or use a third party module)
# to serve assets from there.
STATIC_ROOT = BASE_DIR / "assets"
In you example, it seems you didn't place your files in a directory listed by STATICFILES_DIRS so make sure you have those files:
| manage.py (root of your django app)
| static
| |- css
| | |- bootstrap.min.css
| | |- style.css
To debug this, you can add a print in your settings:
print([os.path.join(BASE_DIR, '/static')])
To fix this, you just have to remove the slash for it to work correctly.
From the os.path.join() docs:
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
That's why BASE_DIR is being cut off from your STATICFILES_DIRS variable.

Django - cant find my static files even after manually correcting the path

I've encountered a problem that, even after an evening of trying , I've not yet been able to resolve.
For my project I have a single static directory, and I've included the css files for the 'login' page in there. Yet when I try to load the webpage I get a 404 error.
When I try to find the files with the 'findstatic' or 'collectstatic' commands the files get skiped. Its like the 'static' directory is invisible to django. I've even directly copies the 'Login' folder to 'static_root', but even then the program was Django was unable to find the files.
Does anyone have an idea what I am doing wrong?
My project folder looks like this:
* MPS (main folder)
* Scraper (app)
* Database (app)
* Templates
* Static_root
* Static
* Login
* css
* style.css
My settings.py has been configured like so:
STATIC_URL = 'static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root/')
And I call the static files in the template with the following code:
{% load staticfiles %}
(...)
<link rel="stylesheet" href="{% static 'login/css/style.css' %}">
Your project folder structure looks a bit different from most Django projects. Traditionally it would look something more like this:
myproject/
urls.py
wsgi.py
settings.py
myapp/
static/
myapp/
css/
style.css
js/
index.js
manage.py
Notice that the app static files of myapp is within a subfolder called the same as the app folder. This is to easily namespace the files as you can read more about in the docs.
Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those static files inside another directory named for the application itself.

Django Appending Slashes to Static File URLs on OpenShift

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' %}"

Django static files not loading (production)

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>

Serving static images on webfaction-hosted django site

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