I have created CSS files in a folder static and currently I'm getting this error 404 when its trying to load the page and no css is being recognised.
my folders are
DEMOPROJECT
-DEMOAPP
--migrations
--templates
---DEMOAPP
-----homepage.html
----base.html
-DEMOPROJECT
--static
---css
----NavMENU.css
--settings.py
--urls.py
My terminal is this
(venv) C:\Users\jferguson\PycharmProjects\WebP1\DEMOPROJECT>
I have tried different settings that every other person has asked. None seem to be working.
THIS is in the urls.py file for static folder.
urlpatterns = [
re_path(r'^admin/', include(wagtailadmin_urls)),
re_path(r'^documents/', include(wagtaildocs_urls)),
re_path(r'', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is in settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
EDIT
My homepage_html has this at the top.
{% extends "../base.html" %}
{% load static %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block body_class %}home{% endblock %}
{% block content %}
Execute collectstatic command as follow
python manage.py collectstatic
In your settings.py file add:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'your_project_name/static') ]
In your case :
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'DEMOPROJECT/static') ]
try this python manage.py collectstatic and restart your server. Hope so this will help you.
Thanks Meha Parekh for the answer!
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'DEMOPROJECT/static')]
This works ^ instead of the following code in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Related
Here is my project structure
I'm trying to display the image from the static folder but it can't be displayed.
I also tried to mention the static folder in the settings.py file as below:
Here is how I'm calling the image from the base.html file.
I'm not sure what is wrong, I tried to search but the google sources can't help.
The correct format of displaying static images in django would be this
{% load static %}
<img src="{% static 'images/heart1.png' %}">
Edit
Put the following lines in your settings.py
STATIC_ROOT =os.path.join(BASE_DIR,'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
add the following to your urls.py
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
#other urls
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You have to use the static template tag in your template
{% load static %}
{% static "images/heart1.png" %}
Need to access static files from common directory as suggested in
https://docs.djangoproject.com/en/3.2/ref/contrib/staticfiles/
command, python manage.py collectstatic
copies to STATIC_ROOT but when referring to it in html, it throws error
Not Found: /static/teststatic/images/product-5.jpg
How to make it work with files from the STATIC_ROOT location ?
I have tried this and has worked
<img src="{% static 'teststatic/static/teststatic/images/product-5.jpg' %}" alt="Trulli" width="500" height="333">
urls.py
from django.urls import path
from . import views
app_name = 'teststatic'
urlpatterns = [
path('tstatic/', views.tstatic, name='tstatic'),
]
views.py
from django.shortcuts import render
# Create your views here.
def tstatic(request):
return render(request, 'tstatic.html', {})
settings.py
DEBUG = True
.
.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = (
('testcss', os.path.join(BASE_DIR, 'testcss', 'static', 'testcss')),
('helpdesk', os.path.join(BASE_DIR, 'helpdesk', 'static', 'helpdesk')),
('teststatic', os.path.join(BASE_DIR, 'teststatic', 'static', 'teststatic')),
(os.path.join(BASE_DIR, 'staticfiles'))
)
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
tstatic.html
{% load staticfiles %}
<!DOCTYPE html>
<html>
<body>
<h2>HTML Image</h2>
<img src="{% static 'teststatic/images/product-5.jpg' %}" alt="Trulli" width="500" height="333">
</body>
</html>
Error :
2021-04-27 12:42:47,744: Not Found: /static/teststatic/images/product-5.jpg
I am trying to access static files in browser but unable to access them. My static settings insettings.py are below
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
My urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', FrontPage.as_view()),
# url('', include(router.urls))
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My folder structure
projectname
projectname
settings.py
urls.py
app1_name
static
bs4
css
cover.css
admin
templates
My template.html
<link href="/static/bs4/css/cover.css" rel="stylesheet">
The above link isn't loading the static file. I'm getting 404 on hitting above url. Please let me know where am I going wrong.
All you need is an update of your settings:
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
Use: <link href="{% static 'bootstrap/css/cover.css' %}" rel="stylesheet">
Make sure to use {% load static %} at the top of your template.html and you are all set.
So for settings up my django static files I added these code in settings.py
STATIC_URL = '/static/'
STATIC_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
So I created A static folder In Base Directory and then I created a folder named css inside of it and I added a file named nav.css into it.
<link rel="stylesheet" href="{% static 'css/nav.css' %}">
But it's not working at all. It gives me error :
Failed to load resource: the server responded with a status of 404 (Not Found)
What I did wrong?
In your settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
I've created django project and in settings.py configured all the necessary assets
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = [
"~/<project_name>/static",
]
I have all of my static files based in the folder static in the project directory. Inside this folder I have css, js, images folders accordingly. In my base template (that is inherited by others) called base.html I have template tag {% load staticfiles %}. I can't figure out why the page still loads without any static attached, because everything seems to be made properly.
Change to this (more in STATICFILES_DIRS setting):
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
And inside your templates fetch static files like this (Django 1.10):
{% load static %}
<link rel="stylesheet" href="{% static 'css/css-file.css' %}">