kmt
kmt

Reputation: 917

Django redirecting to external url that contains parameters (for development)

When developing with Django without a web server (serving directly from Django) I have a problem with external urls that lack the domain part and have parameters.

Let's say I'm using a javascript library that does an ajax call to "/prefix/foo/bar?q=1" (the url is not something I can change). It is not a problem for the production server but only a problem when not using a web server. I can redirect by adding the following pattern to my urlpatters:

(r'^prefix/(?P<path>.*)$', 'django.views.generic.simple.redirect_to', {'url': 'htttp://example.com/prefix/%(path)s'}),

but of course %(path)s will only contain "foo/bar" not "foo/bar?q=1".

Is there a way to handle this problem with Django?

Upvotes: 0

Views: 2458

Answers (1)

Daniel Naab
Daniel Naab

Reputation: 23066

You'll have to write your own redirect:

def redirect_get(request, url, **kwargs):
    if request.META['QUERY_STRING']:
        url += '?%s' % request.META['QUERY_STRING']
    return redirect_to(request, url, **kwargs)

Upvotes: 2

Related Questions