djangoreversepost-redirect-get

Trying to redirect user from a booking page in django


This is my first Django project and I am really struggling. It is a simple booking app where the logged in user is on a page with a form to make a booking and a list of current bookings. I can make the page work and make a booking but the info in the form isnt cleared and if the page is refreshed it makes the booking again. To solve this, I think I need the page to redirect to a success page after the booking is made. When I try to do this, I get a "NoReverseMatch" error and am told that it is "not a valid view function or pattern name."

Here are the url patterns of booking.urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name='home'),
    path("booking/", views.booking, name='booking'),
    path('my_bookings/', views.my_bookings, name='my_bookings'),
    path('edit_booking/<int:booking_id>', views.edit_booking, name='edit_booking'),
    path('delete_booking/<int:booking_id>', views.delete_booking, name='delete_booking'),
    path('success_booking/', views.success_booking, name='success_booking'),
]

Here are some parts of the booking.views.py:

def my_bookings(request):
    bookings = Booking.objects.filter(user=request.user)
    booking_form = BookingForm()
    if request.method == "POST":
        booking_form = BookingForm(data=request.POST)
        if booking_form.is_valid():
            booking = booking_form.save(commit=False)
            booking.user = request.user
            booking.save()
            return HttpResponseRedirect(reverse('/booking_delete/'))

    return render(
        request, 
        "booking/booking_list.html",
        {
            'booking_form' : booking_form,
            'bookings': bookings,
        },
    )


And here is the snippet of booking_list.html that deals with the submit button:

<div class="col-md-4 mb-4 mt-3">
          <div>
            {% if user.is_authenticated %}
            <form id="bookingForm" method="post"
              style="margin-top: 1.3em;">
              {{ booking_form | crispy }}
              {% csrf_token %}
              <button id="BookingButton" type="submit"
                class="btn btn-lg btn-light">Make A Booking</button>
                <!-- <a class="btn btn-light btn-edit" href="{% url 'my_bookings' %}">Make Booking</a> -->
            </form>
            {% else %}
            <p>Log in to make a booking</p>
            {% endif %}
          </div>

I know that I lack understanding of how this all works. I have tried altering the urls file and using DTL for the submit button. I have tried different arguaments within the HttpResponseRedirect() function but I am really lost!!!


Solution

  • There are 2 issue with this line

    return HttpResponseRedirect(reverse('/booking_delete/'))
    
    1. no need to use HttpResponseRedirect.
    2. reverse or reverse_lazy take url name parameter to retrive url not url itself.

    Update this line code should work

    from django.urls import reverse
    return redirect(reverse('booking_delete'))