PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 26, 2022

[FIXED] How can I show my images through JsonResponse in Django's view?

 July 26, 2022     django, javascript, json     No comments   

Issue

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 %}


Solution

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 %}


Answered By - JanMalte
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing