I am developing an agency web app with django templates & static files. But I am having trouble with loading static files on pages other than index.
Here's my folder heirarchy.
Again, I am having no problems loading static files on my index page.
Here's code of my additional page services.html (one I am trying to load static files on):
{% extends 'base.html' %}
{% load static %}
{% block content %}
...
...
...
{% endblock %}
My base.html is working totally fine with the homepage index.html.
I've included this on my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And, my views.py and urls.py files are working fine too.
Also my CSS file is correctly linked to the services.html file. I can tell this from the inspection.
Here's the error list shown on inspection. Help will be appreciated. Thank you.
Your static files are stored in the core folder, so one way is to change the settings.py to :
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'core/static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
The other way is to use python manage.py collectstatic. A new staticfiles directory will be created which has folders for admin (the built-in admin has its own static files), staticfiles.json, and whatever directories are in your static folder.
RESOLVED.
I was apparently not using Django HTML tags to link my static CSS/JS files.
So, I initialized static files on my base.html with {% load static %} and replaced links with href="{% static 'assets/css/style.css' %} format. This worked out. Remember that full path shouldn't be written i.e {% static './static/assets/...' %} instead just use {% static 'assets/...' %}.
Related
I am trying to load the static path of the image which is not working but the static path of the css files are working perfectly . I have tried every different way but still getting 404
this is i am trying to access the image which is inside the folder of ip in static folder
<div class="col-auto">
<img src="{% static 'ip/best.png' %}"/>
</div>
I have load the static block above
{% load static %}
and the settings.py configuraion of static url
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
I believe this is a media file so you need to add this to the project settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
you can add the folder to it and refer to it
I'm having problems with my Django > css its not loading on the page for some reason am I missing something ?
I have attached snippets of my current code
Settings.py
urls.py
.html
put in you'r setting.py
INSTALLED_APPS = [
'django.contrib.staticfiles',
]
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [
STATIC_DIR
]
then create folder static/file.css(nameyourfile.css)
and then put in your ...html :
<link rel="stylesheet" href= "{% static "file.css" %}">
{% load sataticfiles %}
if you use version 3. put :
{% load static %}
You need to do a few more things to configure your static files.
First, you need to make sure django.contrib.staticfiles is included in your INSTALLED_APPS in your django settings.
Also, you need to add this line in your html document:
{% load static %}
I would follow this guide which explains static files in the django docs.
The current project I've started working on, uses django 2.2 and most of the links are hardcoded.
All static content is in folder named media and used in base.html as follows
in base.html
{% load staticfiles %} ---- using this as first line of base.html
.......
.......
<head>
<link href="/media/css/backoffice/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
in settings.py
STATIC_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'media'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'media/')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I've also added following to the main urls.py file:
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Yet I'm not able to render the static file listed in the head section of base.html
I've also experimented with commenting out static root and staticfiles_dir but it does not work
You need to put all files in project root's static folder and run this command:
python manage.py collectstatic
For loading static files in the template you need to write this:
{% load static %}
And in all the static files like css, js and images you can do like this:
{% static "<absolute path of file>" %}
And you can refer from documentation.
I hope this will help you!
I know that this problem has already been asked several times here. I have searched and read a number of answers but no help. I guess, I am missing something very basic.
In my settings.py, I have:
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
# STATICFILES_DIRS = [join(APPS_DIR, 'static')]
MEDIA_ROOT = join(APPS_DIR, 'media')
MEDIA_URL = "/media/"
In my config/urls.py, I have:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I have a file located at /static/core/js/jquery_countdown/jquery.countdown.min.js which I am trying to load in template as below:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="{% static 'core/js/jquery_countdown/jquery.countdown.min.js' %}"> </script>
The top of the same template looks like
{% extends "contest/base.html" %}
{% load static %}
This results in following server error:
[23/Mar/2018 10:12:08] "GET /static/core/js/jquery_countdown/jquery.countdown.min.js HTTP/1.1" 404 1858
What am I missing?
Create folder static_files in your app directory. And place all your static files inside it. Then use the following settings
STATIC_URL = '/static/'
STATIC_ROOT = join(APPS_DIR, "static/")
STATICFILES_DIRS = [join(APPS_DIR, 'static_files')]
If it does not solve your issue, then run the command python manage.py collectstatic. It will copy all the static files (Not only from your app but also from django admin, third party apps etc) to the STATIC_ROOT folder.
Details
For local serving, django server will look in to the STATICFILES_DIRS. So you dont need to run the python manage.py collectstatic command. All the external apps may have STATICFILES_DIRS where they place their static files. For production server you need to collect all these static files scattered around all your apps in to a single place. Thats what python manage.py collectstatic command will do. It will copy all the static files in to STATIC_ROOT directory
You are searching for static files in app directories only but have your file in global static files. You should use the commented STATICFILES_DIRS setting to specify all places to search for static files.
I found that this worked for me. (Development)
(I chose to name it "zStatic" to keep it at the bottom of the root (project folder), so that it isnt amidst all my apps. Easier to locate.)
Settings.py
STATIC_URL = 'zStatic/'
STATICFILES_DIRS = (
BASE_DIR/'zStatic',
)
INSTALLED_APPS =
[
...
'django.contrib.staticfiles',
...
]
base.html (base template)
<head>
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/mycss.css' %}">
</head>
I finally made it to run 'python manage.py collectstatic' to upload staticfiles to my S3 bucket.
But after running 'sudo service apache2 restart', my page doesn't load staticfiles although my S3 Bucket has 'static' directory including all static files.
How can I solve this problem?
I include static files like below :
{% load compress %}
{% load static from staticfiles %}
{% compress css %}
<link rel="stylesheet type="text/css" href ="{% static 'bower_components/.../bootstrap.css' % /}
...
{% endcompress %}
My settings.py is below:
STATIC_URL = S3_URL + '/static/' # which is the path of the static directory included in my S3 Bucket
STATIC_ROOT = STATIC_URL
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
Is there anything I'm missing now?