DEV Community

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay