[Answered ]-Is there any way to redirect to a view from a separate view with arguments for that view function in Django?

1👍

Seems you want to give use redirect and want to give a parameter thats so easy you can do that

x.order_total= total+shipping_total
        x.save()
        return redirect(f'/order/payment/{order_number}')

here i have pasted just a snippet of my one view which is doing that you can insert the parameter using f outside the string if you have matching url it will work fine

0👍

You can redirect to other view with parameter(s) using reverse that doesn’t use hard coded url instead the name of url infact redirect uses that internally

def view_one(request):
    return redirect('view_two', arg=arg)

By default redirect will do temporary redirect for perminant redirect you have to specify it as to know the difference between the two take a look here

0👍

You can use the reverse function to fill your URL pattern
You can import the reverse function with the following line:

from django.core.urlresolvers import reverse

If you have URLs like this you can use reverse:

url(r'^project/(?P<project_id>\d+)/$','user_profile.views.EditProject',name='edit_project'),
path('project/<str:project_id>/', name='edit_project'),

usage:

redirect(reverse('edit_project', kwargs={'project_id':4}))

or:

redirect(reverse('app_name:edit_project', kwargs={'project_id':4}))

Doc here

Leave a comment