Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

How to display the image in Django HTML Page:-


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

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post