Changing static files path to s3 buckt path django - python

Currently I am getting my static files like this:
src="{% static 'website/images/home-slider/plantarte-espacio-4.jpg'%}"
And my settings.py look like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'website/static'),
)
Now wat I want is that instead of grabbing the static files from the static foleder inside my app. It goes to my AWS S3 bucket I created and uploaded all the files to
Instead of this:
src="/static/website/images/home-slider/plantarte-espacio-4.jpg"
Do this:
src="https://plantarte-assets.s3.us-east-2.amazonaws.com/website/images/home-slider/plantarte-espacio-4.jpg"
If someone could please help me I would really apreciate it.

In the simple case, change your
STATIC_URL = '/static/'
to
STATIC_URL = 'https://plantarte-assets.s3.us-east-2.amazonaws.com/'
and you should be golden.

Related

Django CSS Configuration doesn't show expected styling

I'm new to Django, and I'm trying to use static files to color my website.
this is my directory hierarchy-
This is the HTML I'm trying to style, by using this code-
This is the CSS code I'm using-
This my settings.py-
No matter what I do, or if I refresh or restart the server completely- nothing happens.
I've watched so many articles and videos related to this, but I still can't figure out what am I doing wrong...
Would appreciate any help :-)
The way you try to access the static files is correct. But you need to adjust your settings.py:
# djangotemplates/djangotemplates/settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
import os
STATIC_URL = 'static/'
# Add these new lines
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I usually add a STATICFILES_DIRS in my settings.py and it works
STATICFILES_DIRS = [
BASE_DIR / 'to_excel/static'
]

How to create common folder for css and js files in Django?

setting before any change.
STATIC_URL = '/static/'
here I want to create one static files folder rather than assigning them to each every app by creating there name as directory and then I have to assign. So I want one common folder for all JS and Css which can be reflected in each and every template rather than defining all of them at different places.
firstly make below changes in your settings.py file :
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "allfiles"),
]
Remember that allfiles is also folder in your project root.
After that in your template load static files as given below:
inside template.py:
{% load static %}
<html><script src="{% static 'index.js' %}"></html>
<--this file is in your common file you can replace it with any JS or CSS-->

Django: pictures not loading in HTML templates

When I add a random picture inside an HTML it doesn't load at all.
Here is how I set up my Django Project:
Inside of index.html:
{% load static %}
<img src='{% static "img/logo2.png" %}'>
inside of Production, base and local.py I have this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
Thank you in advance if you can help me.
Basic Configuration
STATIC_URL = '/static/'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
In your server try to access http://localhost:8000/static/img/logo2.png and check if the image is propely loaded, if the image loads so your confugiration at settings.py is right.
EDIT: You using heroku? im not sure if you will e able to use your static files with django at heroku, probabily use some external static server as Amazon S3
Here static folder was inside myapp but you have given root
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
place the static folder into settings of blankapp
change the img src in HTML
check now

correct static files setting

Hello I'm very confused about setting static files up. Every thing works fine(displays image, javascript, css) no matter what I try. So I'm confused which one is the right one.
Currently, this is how my project looks like
project
--project
---------static
---------media
--env
--static
--------media
--------static
And this is my code
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static")
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
When I do python manage.py collectstatic, I don't get any error but static folder that's in outer static folder doesn't contain anything. but media folder that's in static folder contains the files in media folder that's in project folder.
Also
I have this for aws,
AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True
AWS_QUERYSTRING_AUTH = True
DEFAULT_FILE_STORAGE = 'project.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'project.utils.StaticRootS3BotoStorage'
AWS_STORAGE_BUCKET_NAME = 'realproject'
S3DIRECT_REGION = 'ap-northeast-2'
S3_URL = '//%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = '//%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + 'static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
import datetime
date_two_months_later = datetime.date.today() + datetime.timedelta(2 * 365 / 12)
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")
AWS_HEADERS = {
'Expires': expires,
'Cache-Control': 'max-age=86400',
}
Can someone please tell me if I'm doing it right?
by the way, I read https://docs.djangoproject.com/en/1.9/howto/static-files/
and followed it, i'm not sure if I followed it right(displayed above) which is why I'm asking.
The python manage.py collectstatic command looks for all your static directories and combines those file in the directory defined by the STATIC_ROOT setting.
In your case, STATIC_ROOT is set to os.path.join(os.path.dirname(BASE_DIR), "static", "static"), i.e.
your_project/static/static
So this is where the static files are being collected to. If you want them in the outer static directory, you can change STATIC_ROOT to os.path.join(os.path.dirname(BASE_DIR), "static").
There is a good discussion of this in the excellent Django docs here.
There is quite a lot to take in in these settings, so here is a quick summary of each static setting as an example:
# this is the URL that django will look for static resources at
# - i.e. http://your_domain/static
# so this one is a URL used when by your web server and in template
# shortcuts.
STATIC_URL = '/static/'
# this is where Django will look for static files to collect.
# I.e. the search locations that collectstatic uses.
# 'my_project/static' in this instance. You need to add the places
# you write your static files to this directory. For example, if you
# have several places where you are writing css files, add their
# container directories to this setting.
# it is a list of places to look for static files.
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
# this is where collectstatic will collect the static files to.
# When you hook this all into your webserver, you would tell your
# webserver that the /static/ url maps to this directory so that
# your app can find the static content. It's a directory in your
# project usually.
# it's a single directory where the static files are collected together.
STATIC_ROOT

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.

Categories

Resources