0

How do I forgo the loop and display only one product represented below?

{data.title }}



 {% for data in data %}
    <h3 class="my-4 border-bottom pb-1">{{data.title }}</h3>
    <div class="row">
        {% endfor %}

I tried to use:

<h3 class="my-4 border-bottom pb-1">{{data.title }}</h3>
    <div class="row">

I got a blank section, the loop works perfectly though. Here is the views function:

booking_detail(request, slug, id):
        booking=Bookings.objects.all().order_by('-id')
        return render(request,'booking_detail.html',{'data':booking})

model

class Bookings(models.Model):
    title=models.CharField(max_length=200)
    image=models.ImageField(upload_to="rooms_imgs")
    slug=models.CharField(max_length=400)
    detail=models.TextField()
    features=models.TextField()
    location=models.ForeignKey(Location, on_delete=models.CASCADE)
    category=models.ForeignKey(Category, on_delete=models.CASCADE)
    hostel=models.ForeignKey(Hostel, on_delete=models.CASCADE)
    amenities=models.ForeignKey(Amenities, on_delete=models.CASCADE)
    roomsizes=models.ForeignKey(RoomSizes,on_delete=models.CASCADE)
    status=models.BooleanField(default=True)
    is_featured=models.BooleanField(default=False)
    is_availabe=models.BooleanField(default=True)
    

url

path('booking/<str:slug>/<int:id>',views.booking_detail,name='booking_detail'),

1 Answer 1

1

As I understand this view function is used only for one booking

booking_detail(request, slug, id):
        booking=Bookings.objects.all().order_by('-id')
        return render(request,'booking_detail.html',{'data':booking})

So it is better to change it to retrieve only one record, not list

from django.shortcuts import get_object_or_404


def booking_detail(request, slug, id):
    booking=get_object_or_404(Bookings, pk=id, slug=slug) 
    return render(request,'booking_detail.html',{'booking': booking})

And then in HTML just:

<h3 class="my-4 border-bottom pb-1">{{ booking.title }}</h3>
<div class="row">

Documentation https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#get-object-or-404

Sign up to request clarification or add additional context in comments.

6 Comments

what do you mean by one booking, and is the get_object_or_404, going to retrieve one record or allow me to get only 1 object, and should I introduce it to the function
Booking is more of a functionality so I have lots fo records under it
@Sik2 do you need to pass the entire list of booking records to the view, or are you going to display a single record on that page? The ideal approach for latter case is to just query one record, like in this answer. If you are displaying more details on the page and need the entire queryset, then the answer will have to be different.
I need a single record, yes I am asking how do I query just one record(using id maybe as the argument)
Then that is exactly what the code in this answer does. get_object_or_404() will fetch the record with the same id and slug values, if it exists. If there is no record, it'll show a 404 (not found) page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.