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'
]
Related
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.
I'm quite new in Django, coming from PHP and node mostly.
By default, I understand that every app needs to have its own static folder, which is ok for me but I also need to have a global static folder to serve resources that are common to all apps.
The problem is if I add to settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "staticfiles"),
]
I achieve the result of having a common global folder, but then the app-level static folders do not work anymore. Is there a way to keep both approaches? Thanks
I am as well very new to django, and dont't fully understand what I am saying, but it appears that I did the same thing and it somehow worked, STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
do you have this under INSTALLED_APPS = [] 'django.contrib.staticfiles' in settings.py, also are you using a database for your project
I have problem with Django static files. Everything works fine but when I want to change something in CSS or an image or JS nothing is happening.
In settings.py I have this
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I have 2 folder of static files, first is static and second one I get after manage.py collecstatic I get another folder (staticfiles) so even when I'm deleting both folders my website files not breaking, I need to restart my Mac. Plus I cannot change elements in CSS because I'm working on both folders but nothing happens.
Directory structure
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 want to display an image on my website. I've been looking through the Django documentation and the other posts on stackoverflow, but I haven't gotten this to work.
I have an image name 'under-construction.jpg'. It lives in the /home/di/mysite/myapp/static/images directory.
I have a template like this:
<img src="{{ STATIC_URL }}images/under_construction.jpg" alt="Hi!" />
in my views.py I have this:
def under_construction(request):
return render(request, 'under_construction.html')
In my settings.py, I have this:
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/home/di/mysite/myapp/static',
'/home/di/mysite/myapp/static/images',
)
I executed ./manage.py collectstatic and it put a lot of files in /home/di/mysite/admin and /home/di/mysite/images. What do I have to do get my image to show up?
All you need to do is edit settings.py to be as the 4 following points and create a new folder (static) in the same folder settings.py is located
in the folder static you can create another folder (images) in it you can put the (under_constructioon.jpg)
STATICFILES_DIRS = (os.path.join( os.path.dirname( __file__ ), 'static' ),)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)
STATIC_URL = '/static/'
STATIC_ROOT = ''
after you're done with the prev. points you can write
<img src="{{ STATIC_URL }}images/under_construction.jpg" alt="Hi!" />
I have faced same problem displaying the static image. suffered a lot and spent a lot lot of time in this regard. So thought to share my settings.
settings.py
STATIC_ROOT = ''
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
import os.path
STATICFILES_DIRS = (
"D:/temp/workspace/offeronline/media",
(os.path.join( os.path.dirname( __file__ ), 'static' )),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ... and at the end of the file add the following line
urlpatterns += staticfiles_urlpatterns()
and finally added the following code to the template tag and it worked
<img src="{{ STATIC_URL }}images/i1.png" />
I have solved that problem like this.
STATIC_ROOT = '/path/to/project/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)
Django will not directly get files directly even you do this. You need to link this static directory for each application of yours. Just go to the django app dir run following command..
cd /path/to/project/my_proj/my_app
ln -s /path/to/project/static/
This will work only in debug mod. You need to set DEBUG = true in settings.py file. This should work with django's development server.
Django won't serve any static file in production mod. You need to serve static files with web-server in production mod.
More information can be found here..
Django does support static files during development, You can use the django.views.static.serve() method in view to serve media files.
But using this method is inefficient and insecure for production setting.
for Production setting in Apache
https://docs.djangoproject.com/en/1.2/howto/deployment/modpython/#serving-media-files
set STATIC_ROOT = /path/to/copy/files/to
have you added
urlpatterns += patterns('django.contrib.staticfiles.views', url(r'^static/(?P<path>.*)$', 'serve'),
or you can also do this
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ...
urlpatterns += staticfiles_urlpatterns()
and of course try not to server static files through django it does have some overhead instead configure your http server to serve them, assuming you have an http server (nginx is quite good).