Home > Enterprise >  User bookings not appearing when logged in
User bookings not appearing when logged in

Time:07-20

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" >
    <div >
        <h2>My Bookings:</h2>
        <p>To edit or cancel a booking please click on the buttons below.</p>
        <div >
            <div >
                {% for booking in bookings %}
                <div >
                    <div >
                        <div >
                            <h4 class='card-title text-uppercase text-center'>{{ booking.date }} at
                                {{ booking.time }}</h4>
                            <h6 >{{ booking.name }}</h6>
                            <div class='card-text'>
                                <p><i ></i> {{ booking.phone }}</p>
                                <p><i ></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')

CodePudding user response:

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.

CodePudding user response:

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