[英]User bookings not appearing when logged in
又是我。
當用戶登錄時,我似乎仍然無法讓我的用戶預訂出現。我知道這可能是超級基本的東西,但我只是沒有看到它。 這是我到目前為止編寫的代碼。 我覺得這方面的新視角可能會有所幫助,因為我已經盯着它看了好幾天了。
提前感謝您提供的任何幫助。
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>
查看.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')
刪除python filter
功能:
my_bookings = filter(self, booking)
為什么my_bookings = filter(self, booking)
是必需的?
booking = Booking.objects.filter(user=request.user)
是過濾當前用戶預訂的查詢。 更改您的代碼,如下所示
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.