Forem

Sami Ullah Saleem
Sami Ullah Saleem

Posted on

How to add upload images in Django?

Steps to upload images

  1. First go to settings and add this under static_url
MEDIA_URL = 'media/' # MEDIA_URL tells Django where to look for uploaded files when in development mode (DEBUG = True)
MEDIA_ROOT = BASE_DIR / 'media' # MEDIA_ROOT tells Django where to store uploaded files when in development mode (DEBUG = True) 
Enter fullscreen mode Exit fullscreen mode
  1. In your root project url, add these lines
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from core.views import frontpage, about
urlpatterns = [
    path('about/', about , name="about"),
    path('admin/', admin.site.urls),
    path('', include('store.urls')),
    path('',frontpage,name="frontpage"), 

]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Enter fullscreen mode Exit fullscreen mode

Now, if you want to access the image url, do this

<img src="{{ product.image.url }} alt="image"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more