I have a Django Website and I am currently working on a delete view that has to be a function-based view for later use. I need to display a name and an image in the template of the data I am deleting at that moment. I had it working as a class-based view but now I need it to be a function-based delete view. The code below works and displays the name but when it comes to the image(which is an image field so I cant just type in a URL in the src) it just displays that little miscellaneous image box on the screen and not my actual image. In the templates, if I put {{ image.url }} it displays nothing. How can I get and image to display in the template from a specific pk in a function-based view.
views.py
def deleterepo(request, pk):
context = {}
context['name'] = Add_Repo.objects.values_list('name', flat='True').filter(user=request.user).get(pk=pk)
context['image'] = Add_Repo.objects.values_list('image', flat='True').filter(user=request.user).get(pk=pk)
obj = get_object_or_404(Add_Repo, pk=pk)
if request.method == "POST":
obj.delete()
return HttpResponseRedirect("/front_page/sheets/")
return render(request, "sheets/repo/delete_repo.html", context)
{{ name }}
<img src="{{ image }}"/>
CodePudding user response:
Instead of using values_list
you can just pass the add_repo
instance to the template.
context = {}
obj = get_object_or_404(Add_Repo, pk=pk)
context["repo"] = obj
Now in the template:
Name: {{repo.name}}
<img src="{{ repo.image.url }}"/>