jericho
jericho

Reputation: 289

How to display clicked item/ image in a new view in django?

Imagine you got a feed of images and as soon as you click on one image it displays it in a new template, kind of like a comment view where the picture is on top and the comments section below.

The only thing I achieved was that I displayed the clicked image in a new tab:

<a href="{{image.body.url}}" target="_blank"> <img style="width:250px; background-color:yellow;flex-shrink: 0;min-width: 100%;min-height: 100%" src="{{ image.body.url }}" /> </a>

It is not what I want though... :/


So I imagined it smth like this:


Hopefully I described it undersandably. Thx in advance :)

Upvotes: 1

Views: 1825

Answers (2)

jericho
jericho

Reputation: 289

thanks to the answer from @pasta1020 I made the following:


template1.html

<a href="{% url 'detail' pk=image.pk %}" >
   <div style="width:250px;height:250px;" >
      <img  style="width:250px; background-color:yellow;" src="{{ image.body.url }}" />
   </div>
</a>


urls.py

url(r'^home/detail/(?P<pk>\d+)/$',views.detail,name='detail'),


views.py

def detail(request,pk):
    imagePk = Image.objects.get(pk=pk)
    imagePkUrl = imageP.body.url

    return render(request, 'template2.html', {'theSrc': imagePkUrl})


template2.html

<img  style="width:250px;height: 250px;background-color:yellow;" src="{{theSrc}}"/>

Upvotes: 0

pasta1020
pasta1020

Reputation: 188

Your question is a little difficult to follow, presumably your images/image urls are coming from a database? If so then pass the primary key of the image into the new view so you can display it in the new template.

Upvotes: 1

Related Questions