Django Images In Media Not Displaying Despite Correct Settings

by Rigg97   Last Updated October 20, 2019 05:26 AM - source

I'm trying to upload an image from a form and display it on another template, however the images in media cannot be found.


class Upload(models.Model):
image = models.ImageField(upload_to='images/')


class HomePageView(CreateView):
    model = Upload
    form_class = UploadForm
    template_name = 'home.html'
    success_url = reverse_lazy('rank')

class RankView(ListView):
    model = Rank
    template_name = 'rank.html'


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


urlpatterns = [
    path('', include('avatars.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


<img src="{{upload.image.url}}">


class UploadForm(forms.ModelForm):

class Meta:
    model = Upload
    fields = ['image']

Inspecting the code in browser, the img url is 'unknown'

Tags : python html django

Related Questions

Django Template not loading

Updated August 18, 2015 17:11 PM

MS Edge not submitting form

Updated September 04, 2017 09:26 AM

Form is not saving user data into database Django

Updated December 03, 2017 00:26 AM

Dynamic, django rendering img tag Javascript/HTML

Updated March 11, 2018 04:26 AM