Receiving the 404 error in getting of static django files - python

Please help to understand what I do wrong.
I have in my settings.py :
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'
And in index.html :
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "/css/table.css" %}">
But I still have the error 404 :
"GET /var/cardsite/cardsite/static/css/table.css HTTP/1.1" 404 1696
I have this file :
ls -la /var/cardsite/cardsite/static/css/table.css
-rw-r--r-- 1 root root 77 Sep 25 16:15 /var/cardsite/cardsite/static/css/table.css
So what is going on?
P.S. My project stored on "/var/cardsite" and I want to make static folder on each application, like in example is default application "cardsite"
thanks

Read this Django_Docs
You must also have a STATIC_ROOT option set before you can use the static files, heres some help
add this to your code:
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'
# Here you can add all the directories from where you want to use your js, css etc
STATICFILES_DIRS = [
# This can be same as the static url
os.path.join(PROJECT_ROOT, "static"),
# also any other dir you wanna use
"/any/other/static/path/you/wanna/use",
]
# This is the static root dir from where django uses the files from.
STATIC_ROOT = os.path.join(PROJECT_ROOT, "static_root")
you will also need to specify it in the urls.py file, just add the following code to the urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
after you add this, run the command:
python manage.py collectstatic
This will copy all the statics you need to the static root dir.

Related

404 Static file not found - Django

I have an issue with django. I recently bought an instance of a shared server and I wanted to move my django website from AWS to this server (which use Cpanel). All worked fine with AWS but when I switched to Cpanel all statics files were missing.
this is my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
my project structure:
my_project
|-app/
|-...
|-views.py
|-db.sqlite3
|-manage.py
|-media/
|-my_project/
|-...
|-settings.py
|-static/
|-main_page/
|-js/
|-my-script.js
I add static files like this:
{% load static %}
<script src="{% static 'main_page/js/my-script.js' %}"></script>
This is the error:
GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)
When I go to the URL of the file it understands it like one of my URLs:
I hope you will help me to solve this issue ;)
thanks.
you need to add the static & media files config in the urls.py , like this
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/

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 admin not serving static files?

Django 1.6
I'm having trouble serving my static files for my Django Admin.
urls.py:
urlpatterns = patterns('',
url(r'^$', 'collection.views.index', name='home'),
url(r'^collection/', include('collection.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
settings.py
...
MEDIA_ROOT = '/Users/me/projectdir/media/'
MEDIA_URL = 'media/'
STATIC_ROOT = '/Users/me/projectdir/static/'
STATIC_URL = 'static/'
...
template (base.html)
<!DOCTYPE html>
<html lang='en-us'>
<head>
<title>Mysite</title>
{% load static %}
{% block links %}
<link href="{% static 'css/bootswatch-simplex.css' %}" rel="stylesheet" type="text/css">
<link href="{% static 'css/custom.css' %}" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="{% static "favicon.ico" %}">
{% endblock %}
<script src="{% static "lib/bootstrap-3.1.1-dist/js/bootstrap.js" %}"></script>
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% static "admin/" %}{% endfilter %}";</script>
</head>
...
Django is serving my admin OK, just without static files: CSS, JS, etc.
Static files for my public-facing pages work fine.
If I change STATIC_URL to '/static/', then the opposite is true: the admin is fine, but my public pages lose their static files.
Here's the weirdest part. If I "view source" of my admin pages in my browser, it shows the correct URL for static pages, for example:
/static/admin/css/base.css
But if I actually follow the link, it changes it to this:
http://localhost:8000/admin/static/admin/css/base.css
I think it's checking for static files relative to localhost:8000/admin/static/ instead of just localhost:8000/static/. It adds an extra "admin" level to the url, like static is part of the domain. I just can't figure out how to get rid of it.
I have tried collectstatic, but it doesn't help. The static files are in my static directory, they're just not being served. I can type in, say, http://localhost:8000/static/admin/css/base.css and I get the right CSS file (in plaintext). The files are there. I bet something is wrong with my configuration.
I've emptied my caches, restarted my dev server, etc. No beans.
ideas?
Use django-admin.py collectstatic or go to ~/django/contrib/admin/static and copy the admin folder(which contains the static files) and paste them into your project's static directory.
**EDIT**
A desperate or clumsy solution you can try for: change your STATIC_URL to '/static/', as from question I saw this:
If I change STATIC_URL to '/static/', then the opposite is true: the
admin is fine, but my public pages lose their static files.
Then check with inspect element/firebug, see what urls are being served in public pages. Probably a '/' missing or added a '/'. Adjust it, and see if it works.
OK, I figured it out. There was some confusion in my settings files, and I did not have STATICFILES_DIRS correctly set.
In the end, I implemented the version-controlled settings files discussed in Two Scoops of Django 1.6, with this in my settings:
from unipath import Path
BASE_DIR = Path(__file__).ancestor(3)
MEDIA_ROOT = BASE_DIR.child('media')
STATIC_ROOT = BASE_DIR.child('static')
TEMPLATE_DIRS = (
BASE_DIR.child('templates'),
)
STATICFILES_DIRS = (
BASE_DIR.child('myapp').child('static'),
)
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
With this, my static files are being served correctly, both in admin and without. My media files, on the other hand, did not work without changing my urls.py in development, according to the accepted answer here. I did not have to do the same for my static files.
Anyways, I hope this helps anyone else banging their head against this particular wall.
I faced the same issue for two times.
The way i solved it was by pasting the static files of admin into static folder mentioned in the code -
cp -r /usr/local/lib/python2.7/site-packages/django/contrib/admin/static/admin /home/ec2-user/mywork-Deployment/mywork/static
This one definitely works and saves a lot of time and troubles.
Hope it helps!
First You need to try:python manage.py collectstatic
then u got any errors just follow these steps
step1
**Remove these code from you**r settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/home/uour project/src/project name/static/',
) //remove these lines
step2
Replace with it replace with these codes
STATIC_ROOT = os.path.join(BASE_DIR, 'static') //add these line
step3
open terminal and type:python manage.py collectstatic
If it helps anyone, I will share what the issue was with my code. Probably my stupid mistake but may save someone's time:
So, basically. my settings.py variables were something like this:
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/"
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"
I inspected the admin page css src tags and found out the URLs were like this:
(Notice two forward slashes in the URL)
https://bucket-name.s3.amazonaws.com//admin/css/login.css.
So I changed my variables slightly and everything loaded fine.
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"
That corrected the faulty URLs and static files loaded perfectly.
The way that worked out for me was I referenced the static admin files in my settings.py file. I hope this helps someone :)
'./static/admin/',
in my case i only set debug to True and they loaded , strange but worked somehow

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.

Static files are not loaded in Django

I'm trying to follow these instructions to enable static files in my Django project.
I have
STATIC_URL = '/static/'
in my settings.py file. I have 'django.contrib.staticfiles', added to the INSTALLED_APPS = (...) part
I created a folder mysite/static/mysite and put my files there. But after I run the server, I cannot access my files at http://127.0.0.1:8000/static/mysite/style.css.
What I have done wrong?
In settings.py include this:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static/mysite/'),
)
And in your urls.py include these lines at the end:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Hope it helps.
I can suggest a couple things to check:
In my projects at least, the static folder is in the app directory, not the project directory. For example, mysite/myapp/static/myapp/img.jpg rather than mysite/static/mysite/img.jpg. Your project might not be looking in the right place.
Make sure that {% load staticfiles %} is in your html template before linking the files.
When you link a file in your html template, rather than direct urls like
<link rel="stylesheet" href="myapp/css/custom.css">
use
<link rel="stylesheet" href="{% static 'myapp/css/custom.css' %}">
This was enough to get static files working in my projects, without having to modify urls.py or manually set PROJECT_ROOT or STATICFILES_DIRS.

Categories