How to get url of multiple images stored in folder in Django

I am developing a django app which converts pdf to images.I am using FileField for this purpose and using ModelForms. Once uploaded the pdf files will get converted into .png and stored in a separate folder. The upload and converting pdf to png works fine but now I want to display the converted images.

I know I can use url of the uploaded image to display it but the issue is I am uploading a pdf file and hence cannot use the url to display it. I know how to display an uploaded image using url of that image. But I am uploading a pdf file, converting into image and finally storing it into separate folder. So how can I display the images stored? Below is the code I am using:

from django import forms
from django.forms import ClearableFileInput
from app1.models import UserUploadModel

class UserUploadForm(forms.ModelForm):

    class Meta:

        model = UserUploadModel
        fields = ['file']
        widgets = {
            'file' : ClearableFileInput(attrs = {'multiple': True}),

from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class UserUploadModel(models.Model):
    user = models.ForeignKey(User, on_delete = models.CASCADE, null = True)
    file = models.FileField(upload_to = 'file_uploads')

from django.shortcuts import render, redirect
from app1.forms import UserUploadForm
from app1.models import UserUploadModel
from app1.convert import convert_file
from app1.transfer import move_dir
import os
from project1 import settings
from django.contrib.auth.decorators import login_required

def home(request):
    if request.method == 'POST':
        form = UserUploadForm(request.POST, request.FILES)
        if form.is_valid():
            files = request.FILES.getlist('file')
            f_list = []
            if files:
                for i in files:
                    file_instance = UserUploadModel(file = i, user = request.user)
                [convert_file(j) for j in f_list]
                src_dir = os.getcwd()
                dest_dir = os.path.join(src_dir, 'media/converted_files')          
                move_dir(src_dir, dest_dir, '*.png')                
            return redirect('app1-display')
        form = UserUploadForm()
    return render(request, 'app1/home.html', {'form' : form})

def display(request):
    images = UserUploadModel.objects.filter(user = request.user)

    context = {
        'images' : images
    return render(request, 'app1/display.html', context)


{%extends "app1/base.html"%}
{%block content%}

    <form method="POST" enctype="multipart/form-data">
      <input type="submit">

{%endblock content%}


{%extends "app1/base.html"%}
{%load static%}
{%block content%}

    {%if user.is_authenticated%}
        <h2>User: {{request.user.username}}</h2>

    {%if images%}
        {%for i in images%}
            <img src={{ i.file.url }}>
{%endblock content%}

Wrong approach change your model and make a file field for the converted pdf also. After converting save the file to this model field. Then you can get the url of the converted file.

I'm not sure but can you use PIL to open the image and then use Django File Response to return the image. from PIL import Image from django.http import FileResponse

def display(request): images = UserUploadModel.objects.filter(user = request.user) if images: image =[0].file.path) return FileResponse(image, content_type='image/png')

Back to Top