Ad Code

✨🎆 Codex 1.0 PLACEMENT READY PROGRAM! 🎆✨

Get 75% Discount Early bird offer CLICK to JOIN CodeX 1.0 click

How to display image in django HTML Page local and global both.




Django provides two different ways to put the image.

1 under app means local scope:-

 create static/img folder under the current app and put an image

use <img> tag of html and write path

<img src="/static/img/img3.jpg" width="500" height="500" />


2 under application means global scope:-

If we want to manage image under application then we can create media director and write the image into this and provide a media path


1)  open the setting.py file and write this code:-

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


2) open urls.py file and write this code:-

from django.conf import settings
from django.conf.urls.static import static


and write code under the below section

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



The complete code of urls.py  will be this:-


from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('frontdesk/', include('frontdesk.urls')),
    path('admin/', admin.site.urls),
]


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


3) Code for images under html file



<img src="../media/job.png" width="500" height="500" />

  

We can also put an audio file, video file, pdf file, text file, etc?




 

Post a Comment

0 Comments