Home > Mobile >  Why files from static not shows on page?
Why files from static not shows on page?

Time:02-11

I made for test such simple project in django

I've created views

from django.shortcuts import render

def home(request):
    return render(request, 'app1/home.html', {})

Further i've created url

from courseshop import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from app1 import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home')
]

if settings.DEBUG:
    urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I add some things in settings for static

import os

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = []

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

And i've made template home.html

<!DOCTYPE html>
<html>
    <head>
        <title>Мой сайт</title>
        <meta http-equiv="Content-type" content="text/html; " charset="utf-8">
    </head>
    <body>
        <h1 align="center">Привет всему миру!</h1>

        <p>Регистр (цифровая техника) — последовательное или параллельное логическое устройство, используемое для хранения n-разрядных двоичных чисел и выполнения преобразований над ними.</p>
       
        <img src="app1/images/stars.jpg"
        <hr>
    </body>
</html>

And i don't see image stars on my page. Why? Please, help me

CodePudding user response:

plz add templates name in settings.py file

TEMPLATES = [ {

    'DIRS': [BASE_DIR / 'templates'],
    ...
},

]

plz check all path

.html img path views.py

CodePudding user response:

when you want to use static files in your template, you should add load static in the beggining of your template and use it like this:

{% load static %}
<img src="{% static 'app1/images/stars.jpg' %}"
  • Related