Krishan Kumawat
Krishan Kumawat

Reputation: 29

when i click a images then show 3 images on other html page but these 3 images is different for every clicked images

i want to access related class object through value class because img field of value class relates with 3 images of related class by Foreign Key

this is my models.py

class value(models.Model):
    img = models.ImageField(upload_to='images/')
    price = models.IntegerField()

class related(models.Model):
    u_id = models.ForeignKey(value,on_delete=models.CASCADE)
    image=models.ImageField(upload_to='images/')
    loc = models.CharField(max_length=20)


this my views.py

def home(request):
    rels = value.objects 
    return  render(request,'rel/home.html',{'rels':rels})

def detail(request,id):
    det = get_object_or_404(value,pk=id)
    det1 = det.id
    u_id = related.objects.get()
    return render(request,'rel/detail.html',{'det':det,'u_id':u_id})

this my html page 
{% for u in u_id.all %}
        <div class="container">
                <img src="{{ u_id.image.url }}" alt="">
        </div>
        {% endfor %}

this is my error :- get() returned more than one related -- it returned 3! (on browser window )

i want that when i click a image which is available on my home.html then show 3 images on other html page but condition is that these 3 images is different for every images which is available on home.html

Upvotes: 1

Views: 41

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

get() returns the single object so you need to use filter() here like this:

def detail(request,id):
    det = get_object_or_404(value,pk=id)
    u_id = related.objects.filter(u_id=det)
    return render(request,'rel/detail.html',{'det':det,'u_id':u_id})

And in template you don't need to use u_id.all just for u in u_id will work

Upvotes: 1

Related Questions