0

How to I redirect to the same url but append a query_string to it? ie. redirect from "base.com/amt" to "base.com/amt/?ref=blah&id=blah"

urls.py

url(r'^amt/$', RedirectView.as_view(reverse_lazy('amt')), {'SSL':True}),
url(r'^amt/?ref_source=AMT&ref_id=PC', 'core.views.show_amt_page', {'SSL':True}, name='amt'),

in this case I want to go from "^amt/$" to "^amt/?ref_source=AMT..."

Once the redirect is done, I need to execute show_amt_page() int views.py

If there's a better way to do it, can someone explain how that can be done so django executes show_amt_page() in views.py?

1
  • The query string is not part of the url definition, so your second url is useless. Commented Mar 10, 2016 at 22:47

2 Answers 2

2

The query string is not handled by the url patterns, therefore you can only have one url pattern for /amt/, which will handle requests with and without the get parameters.

url(r'^amt/', 'core.views.show_amt_page', {'SSL':True}, name='amt'),

In the show_amt_page view, you can check for the get parameters in request.GET, and return a redirect response if necessary.

from django.shortcuts import redirect

def show_amt_page(request, SSL):
    if 'ref_source' not in request.GET:
        return redirect('amt/?ref_source=AMT&ref_id=PC')
    # rest of view goes here
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

in urls.py

...

    url(r'^$', pages.home, name='home'),
...

Function tu build an urls with GET parameters

from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
def build_url(*args, **kwargs):
    get = kwargs.pop('get', {})
    url = reverse(*args, **kwargs)
    if get:
        url += '?' + urllib.urlencode(get)
    return url

Use example

return HttpResponseRedirect(build_url('home', get={'ref_source': 'AMT','ref_id':'PC'}))

return HttpResponseRedirect(build_url('home', get={'opcion': '1'}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.