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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.