Rendering request after redirecting post

Hi,

I’m trying to direct a page from index.html to pay_page.html. But where there is redirect(‘pay_page’), it doesn’t transfer to pay_page.html but get a page error saying, NoReverseMatch at/sign_up.

More about the error: Reverse for ‘pay_page’ not found. ‘Pay_page’ is not a valid view function or pattern name

Could someone tell me what I’m doing wrong?

Here’s my views.py:

from django.shortcuts import render, redirect
from django.core.mail import send_mail

def index(request):
    return render(request, "index.html")

def sign_up(request):
    password = request.POST['password']
    return redirect('pay_page')

def pay_page(request):
    return render(request, "pay_page.html")

Here’s my url.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),
    path('sign_up', views.sign_up),
    path('pay_page', views.pay_page),
]

Hi @Aimslee,

When you pass a name to redirect, like redirect('pay_page') a reverse lookup will be performed. In order for this to work the way you have it, you’ll need to add a name argument to your path call in the urls file.

Example: path('pay_page', views.pay_page, name='pay_page')

See Named URL Patterns in the Django documentation for the full details.

1 Like

It works!! Thank you so much!! :slight_smile:

1 Like