Categories
Python Answers

How to return redirect() with parameters with Python Django?

Spread the love

To return redirect() with parameters with Python Django, we can call redirect.

For instance, given that we have

url(r'element/update/(?P<pk>\d+)/$', 'element.views.element_update', name='element_update'),

in urls.py, we write

from django.shortcuts import redirect
from .models import Element


def element_info(request):
    # ...
    element = Element.object.get(pk=1)
    return redirect('element_update', pk=element.id)

def element_update(request, pk)
    # ...

in views.py to redirect to the view with name element_update in the element_info view.

The pk argument is value of the pk URL parameter.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *