Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
How can I show my images through JsonResponse in Django's view?

I have a question, my collaborator's code works well, it shows some images that were previously uploaded through an input through a modal. The problem is that I want to display the images in a detail view. I modified it and I can only show one of the 10 that were uploaded. How can I show all 10? I have no idea how to handle that JSON he used

views.py


class detail_carro(DetailView):
    template_name = 'carros/carros-detail.html'
    queryset=Carro.objects.all()
    context_object_name = 'carros'


def create_carros_picture(request):

        if request.FILES['files']:
            file = request.FILES['files']
            fs = FileSystemStorage()  # defaults to   MEDIA_ROOT
            new_name = "picture"
            new_name = fs.get_valid_name(new_name)+".jpg"
            filename = fs.save(new_name, file)
            return JsonResponse({filename:file.name},safe=False)
        else:
            form=CarroForm()
            return render(request, "carros/carros-form-add.html",{'form':form})


def create_carros_warranty(request):
    if request.FILES['files']:
        file = request.FILES['files']
        fs = FileSystemStorage()  # defaults to   MEDIA_ROOT
        ext = file.name.split('.')[-1]
        new_name = "warranty"
        new_name = fs.get_valid_name(new_name) + '.' + ext
        filename = fs.save(new_name, file)
        return JsonResponse({filename: file.name}, safe=False)
    else:
        form = CarroForm()
        return render(request, "carros/carros-form-add.html", {'form': form})
 

carros-detail.html

{% if carros.new_name %}
<a data-id="{{carros.id}}" class="btn_view_gallery">
<img src="{% get_media_prefix %}{{carros.new_name}}" height="300">
</a>
{% endif %}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The DetailView is used to show only a single model instance, Carro in your case I think.

To show all model instaces, use the generic ListView.

https://docs.djangoproject.com/en/4.0/ref/class-based-views/generic-display/#listview

views.py

class CarroListView(ListView):
    template_name = 'carros/carros-list.html'
    queryset=Carro.objects.all()
    context_object_name = 'carros'

carros/carros-list.html

{% for carro in carros %}
<a data-id="{{carros.id}}" class="btn_view_gallery">
<img src="{% get_media_prefix %}{{carros.new_name}}" height="300">
</a>
{% endfor %}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!