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" %}
Related
Django is looking for my static files in the wrong place and I dont understand why
Not Found: /account/login/style.css
[26/Feb/2021 19:27:16] "GET /account/login/style.css HTTP/1.1" 404 2434
but my file should be in /static/css/style.css
This is the code in my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'staticfiles')
]
I use staticfiles folder to place my static files afterwards I do py src\manage.py collectstatic to transfer my files in static folder
This is my layout of the project:
In my template I am using {% load static %} and I try to load the style.css with {% static 'css/style.css' %}
Here is the urls.py in core:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('account/', include('accounts.urls', namespace='accounts')),
]
if settings.DEBUG:
# test mode
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urls.py in accounts:
app_name = 'accounts'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name="registration/login.html",
authentication_form=UserLoginForm), name='login'),
]
Login page is the one that I have the problem with http://127.0.0.1:8000/account/login/
Template:
DEBUG is true in settings
Do you have any idea what could be the problem?
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.
my template didn't work and can't get static files but I did everything.
I just can't get style.css but other files are loaded. only style and some js files isn't loaded but the path is not wrong . please help me
this is an image of my folder and project tree
Settings for Static and media.
STATIC_URL = '/temfiles/'
MEDIA_URL = '/mediafiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploads/')
X_FRAME_OPTIONS = 'SAMEORIGIN'
my project URL file
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('froala_editor/', include('froala_editor.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
my app URL file
urlpatterns = [
path('', views.index, name = 'index'),
path('<slug:slug>/', views.post_detail, name='Post Detail')
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my Views File
def index(request):
Post_list = BlogPost.objects.all()
template_name = 'front/index.html'
return render(request, template_name,{"Post_list":Post_list})
def post_detail(request):
return render(request, 'front/post_detail.html')
my base template CSS example
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static
'front/css/style.css' %}">
Static Directory Tree image
static directory tree with open folders
browser inspect terminal picture
style CSS URL in browser
MY urls.py for static and media root
Django STATIC files does not work in Develop mode.
I tried in many ways, but I did not succeed.
Could someone tell me where I'm wrong?
Thank you very much for your attention.
STATIC_URL = '/static/'
STATIC_ROOT = "/Users/user/Projects/RMA/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '..', 'static'),
)
....
'django.contrib.staticfiles',
routes
urlpatterns = [
url(r'^$', views.index, name='index')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
View
def index(request):
return render(request, 'base.html', {'question': 'ads'})
template
{% load static %}
<link href="{% static 'site/bower_components/select2/dist/css/select2.min.css' %}" rel="stylesheet">
I am trying to use static images in my template using the DJANGO framework, but the images don't display on my page.
I have followed the manuals completely.
Does anyone have any ideas?
Here the code :
html :
{% load staticfiles %}
div class="portfolio-item">
<img class="img-portfolio img-responsive" src="{% static portfolio2.jpg %}">
</div>
settings.py :
STATIC_URL = '/static/'
STATICFILES_DIRS=(
os.path.join(BASE_DIR, 'static','static_dirs'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static','static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static','media')
MEDIA_URL = ('/media/')
urls.py
+
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Try: quotes 'portfolio2.jpg' before file
<img class="img-portfolio img-responsive" src="{% static 'portfolio2.jpg' %}">