Unable to join static file to root directory in Django - python

To join the static file in Django,
STATIC_URL = "os.path.join(BASE_DIR, '/static/')"
print("static file : ", (os.path.join(BASE_DIR, '/static')), )
This is producing :
static file : C:/static
But in the same document I had joined template folder:
print("Path is : ", os.path.join(BASE_DIR, 'myproject/template'))
Which produces this: Path is : C:\Users\user\Desktop\django\myproject\myproject/template
Is this the reason Django is producing this error:
ERRORS:
?: (urls.E006) The STATIC_URL setting must end with a slash.
Could you please advise how can i resolve this error?

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL is the path on your website url.
STATIC_ROOT is the path on your server where static files are located.

Related

Media file not being found in production Django-Heroku: returns 404 error

I have some default images in my media folder in Djang. However, on my website, I cannot see the images being displayed and I get a 404 error.
Now my project folder tree is(project name is register):
register
-- live-static
---- static-root
---- media-root
------ defaults
and my media and static files settings are:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'live-static', 'static-root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'live-static', 'media-root')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
And inside my urls.py in the same folder as wsgi.py I have:
urlpatterns = [
"""
My other urls
"""
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My static files are loaded and the server can retrieve them, however, the images that I added to my folders in media-root are returning a 404 error by the server. Why is this happening as I have already also set STATICFILES_STORAGE as well?
This is something I got hung up on with my first Heroku deploy. Since Heroku projects run on dynos, they eventually go to sleep if there is not a request within a certain period of time. When the dyno restarts, it does not save the uploaded data. Here is a link to some information from Heroku: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted
What I ended up doing is setting up an Amazon S3 bucket for storage of these files. Dennis Ivy did a video explaining how to do this: https://youtu.be/inQyZ7zFMHM
Update your setting file like this
there is no need to change any thing else
in urls.py change the urlpatterns to. remove STATIC_ROOT etc
+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in setting.py like this. remove other file settings like STATICFILES_STORAGE
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
#if your media folder is in public_html which contain the media file
MEDIA_ROOT = '/home/yourusername/public_html/media'
STATIC_ROOT = '/home/yourusername/public_html/static'
#if your media folder is in other_folder which contain the media file
MEDIA_ROOT = '/home/yourusername/other_folder /media'
STATIC_ROOT = '/home/yourusername/other_folder /static'

How to load static files in python django

I have tried to modify in the settings.py file to allow loading css files but some of html code still doesn't appear with its style, the run in the terminal the command "python manage.py collectstatic" and it says that files are copied but still doesn't appear its effect.
HERE IS THE MODIFIED LINES IN settings.py file
STATIC_ROOT = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_URL = '/static/'
In your settings.py provide:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_files'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
STATIC_URL = 'http://example.com/static/'
So if static files finders are defined and static_files is declared as source for
static files, whatever you place in
/your_root/static_files/
will come into
/your_root/static
after you run
python manage.py collectstatic
The mistake with
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
is that you're trying to take from and bring to the same directory, which is the destination directory of all static files collected.
It is better to set STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
If you have a folder called static in BASE_DIR, add
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
You may add static_root/ to .gitignore file after you run python manage.py collectstatic
The way I use is:
Create a static/name_of_app directory where I store my static files
2.In Html
first load static files {% load static %}
then link to the path
href = " {% static 'name_of_app/fileName ' %} "
You can add these codes in your settings.py in latest django version
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_ROOT = os.path.join(VENV_PATH, 'media_root')

ImproperlyConfigured("Empty static prefix not permitted")

I am using Django Rest Framework to upload images.
The upload is working fine and is getting uploaded under media folder.
But I am unable to make the media links browsable. It says Error 404.
To make them browsable these are the changes that I made.
In settings.py I added the following lines.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/media/'
In url.py I added the following lines
if settings.DEBUG is True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
DEBUG is True in settings.py as it's not in production. It is on my local machine under development.
But when I am starting the server it is showing me the following error:-
ImproperlyConfigured :- Empty static prefix not permitted
Sorry guys! I found my own mistake. Instead of MEDIA_URL I was using STATIC_URL

STATIC_ROOT setup in Django 1.8

I was tried n I can't set-up as per official documents...
I am attaching IMG here, pls give me suggestions, Where is the problem.enter image description here
Or, give me simple steps for it with dictionary tree structure.
Thank you.
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root', 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
# '/var/www/static/',
)
STATIC_ROOT = 'os.path.join(BASE_DIR, 'static_root', 'static') can't work.
Try this :
# define your base directory
# It will be `absolute/path/to/demo3`
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
# define where your static files will be collected
# It will be `absolute/path/to/demo3/static`
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# keep it empty for the moment
STATICFILES_DIRS = (
)
You have to understand STATICFILES_DIRS role :
Your project will probably also have static assets that aren’t tied to
a particular app. In addition to using a static/ directory inside your
apps, you can define a list of directories (STATICFILES_DIRS) in your
settings file where Django will also look for static files.
I recommend you to read carefully Django docs : Managing static files
STATIC_ROOT should be without quotes:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Also, the STATIC_ROOT folder shouldn't be named the same as the STATICFILES_DIR folder, so you should name it something like staticfiles instead:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATIC_ROOT = 'os.path.join(BASE_DIR, 'static')' is a string which is wrong.
it should be STATIC_ROOT = os.path.join(BASE_DIR, 'static')
The error is clear in stating the STATIC_ROOT is not a filesystem path. Django requires that STATIC_ROOT be an absolute path to a folder location on the machine. This error likely means that your resulting STATIC_ROOT is a partial or relative path. What is the value of BASE_DIR? After the line that sets STATIC_ROOT and a print(STATIC_ROOT) to see what value it is.
Try setting BASE_DIR as follows:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Other Errors
On the line
STATIC_ROOT = 'os.path.join(BASE_DIR, 'static')'
You have the value surrounded by single quotes. os.path.join() is a function call. Remove the quotes and change the line like this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Also, STATIC_ROOT cannot be included in the list of files in STATICFILES_DIRS. Consider setting the STATIC_ROOT folder to:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
when creating the project with latest version of python and django in settings.py file by default from pathlib import Path Statement is there.So in that case no need to use os.path.
from pathlib import Path # Import First
STATIC_ROOT = Path.joinpath(BASE_DIR, 'static_collected')
I added one line code and it worked for me, I hope it doesn't pain in the future :)
import os
Remember to create a static folder in your root directory and also create the static folder in your project folder. Then your file structure would be:
demo3
/demo3/staticfiles # collection folder for all static files
/static # root static folder
/userForms/static # your app static files
# OR
demo3
/demo3
/static_files # collection folder for all static files
/static # root static folder
/userForms/static # your app static files
Add this to your settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'demo3', 'staticfiles')
OR depending on where you want the collection folder
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
Run python manage.py collectstatic
Then it would bundle all static files in project staticfiles OR root static_files folder, depending on where you want them.

I can't define STATICFILES_DIRS using Python to figure out the path

I want to be able to define my settings for static/media files using python to get the paths so I don't need different settings on my dev machine and my server.
So I have these settings;
import os
from unipath import Path
### PATH CONFIGURATION
# Absolute filesystem path to the top-level project folder
SITE_ROOT = Path(__file__).ancestor(3)
### MEDIA CONFIGURATION
MEDIA_ROOT = SITE_ROOT.child('media')
MEDIA_URL = '/media/'
### END MEDIA CONFIGURATION
### STATIC CONFIGURATION
STATIC_ROOT = SITE_ROOT.child('static')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
My problem is that locally it won't load the static files and the terminal says that STATICFILES_DIRS should not contain the STATICFILES_ROOT.
Is it possible to get Python to load the paths like this or am I wasting my time?
There's nothing wrong with your code per se, it's just that the point of the staticfiles app is to copy the files from the directories specified in STATICFILES_DIRS into the directory specified in STATIC_ROOT, so it doesn't make much sense to include the STATIC_ROOT directory in the STATICFILES_DIRS setting.
Unless you're actually using the staticfiles app with ./manage.py collectstatic, you may as well just leave the STATICFILES_DIRS setting empty, i.e. just change...
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
...to...
STATICFILES_DIRS = ()
Do like this:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static/'),
)
That should work. Hope it helps!
+1 for both other answers. If you get tired of typing os.path.bla a lot here's a shortcut you can position at the top of your settings file (or import from anywhere else)
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
STATICFILES_DIRS = (
rel('static'),
)

Categories

Resources