gornvix
gornvix

Reputation: 3384

Parameters not being sent to a URL in Django redirect

I want to send a get parameter to a specific URL e.g. http://127.0.0.1:8000/foo?myid=99

However this code is not creating the get parameter:

def BarView(request):
    return redirect(reverse('foo'), myid=99)

But the resulting URL is just http://127.0.0.1:8000/foo/

def FooView(request):
    return HttpResponse('''<html lang="en"><body><p>myid: %s</p>
        </body>''' % request.GET.get('myid', ''))

In "urls.py":

urlpatterns = [
    path('foo/', views.FooView, name='foo'),
    path('bar/', views.BarView, name='bar'),
]

Upvotes: 0

Views: 61

Answers (1)

Lemayzeur
Lemayzeur

Reputation: 8525

reverse will render your URL name as a string. So if you want to add a get parameter, just concatenate it with a string like the following:

from django.http import HttpResponseRedirect

def BarView(request):
    query = "?myid=99"
    return HttpResponseRedirect(reverse('foo') + query)

Note that I used HttpResponseRedirect

Upvotes: 1

Related Questions