User bookings not appearing when logged in

Viewed 47

me again.

I still can't seem to get my user bookings to appear when the user is logged in. I know it is probably something super basic but I am just not seeing it. Here is the code I have written so far. I feel like a new set of eyes on this might help as I have been staring at it for days now.

Thank you in advance for any help you can give.

HTML:

{% extends "base.html" %}

{% block content %}

<section id="booking" class="book_a_table">
    <div class="booking_container">
        <h2>My Bookings:</h2>
        <p>To edit or cancel a booking please click on the buttons below.</p>
        <div class="row">
            <div class="col-md-3 mt-5 offset-md-3">
                {% for booking in bookings %}
                <div class="col-md-4">
                    <div class="card mb-4">
                        <div class="card-body">
                            <h4 class='card-title text-uppercase text-center'>{{ booking.date }} at
                                {{ booking.time }}</h4>
                            <h6 class="text-uppercase">{{ booking.name }}</h6>
                            <div class='card-text'>
                                <p><i class="fas fa-phone"></i> {{ booking.phone }}</p>
                                <p><i class="far fa-envelope"></i> {{ booking.email }}</p>
                                <p>Number of People: {{ booking.number_of_people }}</p>

                                {%endfor%}

                            </div>
                        </div>
                    </div>
</section>

View.py:

class ListBookingView(generic.ListView):
"""
This is the view that will bring up the
list of bookings for a particular users
so that they can be edited or deleted
"""
model = Booking

template_name = 'my_bookings.html'

def get(self, request, *args, **kwargs):
    if request.user.is_authenticated:
        booking = Booking.objects.filter(user=request.user)
        my_bookings = filter(self, booking)

        return render(request, 'my_bookings.html', {
            'my_bookings': my_bookings
        }
        )
    else:
        return redirect('account_login')
2 Answers

Remove the python filter function:

my_bookings = filter(self, booking)

Edit

There's a context/template mismatch:

# template
{% for booking in bookings %}
# context
{ 'my_bookings': my_bookings }

These need to be the same in order for the bookings to appear.

why my_bookings = filter(self, booking) is required ?

booking = Booking.objects.filter(user=request.user)

is the query which filters the current users booking. change your code as shown below

def get(self, request, *args, **kwargs):
    if request.user.is_authenticated:
        bookings = Booking.objects.filter(user=request.user)

        return render(request, 'my_bookings.html', {
            'bookings': bookings })
    return redirect('account_login')
Related