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
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" %}
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.
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')
I am using Django 1.9.3. I am trying to display media files uploaded by say a user (e.g. imageFields).
HTML Page:
{% for project in projects %}
<img src="{{ project.image.url }}" class="img-responsive" alt="">
{% endfor %}
Views.py:
def home(request):
projects = AvailableProject.objects.all().order_by('-published_date')
return render(request, 'accounts/home.html', {'projects': projects})
urls.py:
urlpatterns = [
...
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
('global', os.path.join(BASE_DIR, 'smilesite', 'project_static')),
('admins', os.path.join(BASE_DIR, 'admins', 'static')),
('accounts', os.path.join(BASE_DIR, 'accounts', 'static')),
('mysite', os.path.join(BASE_DIR, 'mysite', 'static')),
('media', os.path.join(BASE_DIR, 'media')),
)
# print(STATICFILES_DIRS)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
# Define place to save media (e.g. pictures for all the projects)
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'
models.py:
from django.db import models
from django.conf import settings
# Create your models here.
class AvailableProject(models.Model):
image = models.ImageField(upload_to=settings.MEDIA_ROOT)
I have literally went through every single stack over flow page on this and tried the following solutions, but still has not worked...:
<img src="{% static 'media/{{project.image.title}}.jpg' %}" class="img-responsive" alt="">
<img src="{{ project.image.url }}" class="img-responsive" alt="">
I don't know why the project.image.url is not showing the corresponding image correctly.
The reason was exactly what Joni Bekenstein said.
I changed this one line and everything worked:
image = models.ImageField(upload_to='uploads/')
Then {{ project.image.url }}, was the correct url.
I have spent the last few days figuring out how to include an css file into a Django template. I still did not succeed so am hoping someone can help me out.
I have the following settings:
--settings.py--
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
I have not set anything in STATICFILES_DIRS() either.
--urls.py--
urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += staticfiles_urlpatterns()
--views.py--
def my_homepage_view(request):
return render_to_response('test.html', context_instance=RequestContext(request))
--test.html template--
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>
--source code localhost--
<link rel="stylesheet" type="text/css" href="/static/css/style.css"/>
According to the Django documentation it seems that I have set everything correctly, but the css style is still not applied. The static folder is in the correct place (C:reviews/reviewsite/static) where the rest of my apps also reside. Even if I hardcode the style.css location (C:reviews/reviewsite/static/css/style.css) in the test.html template the css style is not applied. I have checked the style.css and it works without Django.
Any idea of what I am doing wrong?
This is how you call the files in the static
{% static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>
settings.py
import os
import sys
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
//create staticfiles folder
os.path.join(SITE_ROOT, 'staticfiles'),
)
Everyone, thanks for your help and sorry for the late reply. I tried your suggestions but it didn't work unfortunately. However, after some more time trying I got it working now. This is what worked for me:
--settings.py--
MEDIA_ROOT = ''
MEDIA_ROOT = ''
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = 'http://localhost:8000/static/'
--urls.py--
urlpatterns = patterns('', (r'^$', 'reviewsite.views.my_homepage_view'),)
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }), )
I now have placed the css file in the static folder in my apps directory. In the template I am using {{ STATIC_URL }}/style.css.