I have a webpage with lots of data displayed. Thge page was created using Django 1.6, class-based views and templates. When the user clicks on one fo the data elements displayed, another views gets invoked. That view then generates a list of parameters that are then passed, via URL parameters, to another, non-Django based webpage (for example, amamzon.com). Is there a way to do this? Currently I invoke a second page with a button on it that contains an 'href' to the non-Django page.
-
I just ran across this posting. Seems like it might work, but requires formatting an entire webpage just to redirect away from it. [link](http://stackoverflow.com/questions/1345892/external-django-redirect-with-post-parameters). – user3130740 Dec 16 '14 at 19:08
3 Answers
use redirect from django.shortcuts. to is passed as is to location header of redirect response.
from django.shortcuts import redirect
def my_view(request):
# somehow building my url
return redirect(to=my_url)

- 1,349
- 11
- 16
-
If I use redirect it goes through my urls.py file to dispatch. But my target URL is not defined in urls.py since it is not part of my project/application. – user3130740 Dec 16 '14 at 18:02
-
I have a ListView class that I am using. I override both the get_queryset() and get_context_data() methods currently while building my results. I put the redirect in get_context_data() as you described but it just returns to the default page (I assume because a template_name is specified for the class). The redirect I'm trying to use is like this: return redirect(to="https://nnn.com/default.aspx?list=13621189%271418954%271448392%271588175%271588260%27200030267%29200063727") – user3130740 Dec 16 '14 at 18:08
-
I also found that this works. I created a template with just this in it to redirect. The View code then picks which template (this one or the one that lists the results). I tested it on IE 9.0, FF 34 and Chrome. It works, but is not very eloquent. – user3130740 Dec 16 '14 at 20:02
I eventually:
- Created a Template with just this code in it.
HEAD META HTTP-EQUIV="Refresh" CONTENT="0;URL={{ my_url }}?parms=-{{ parm_data }}" /HEAD
- In the Views code I added a check to see if I should use the standard template within Django, or go to the non-Django website. If I want to go to the external site, I just changed the template_name for the class to the new template and returned.
if not self.stay_internal: self.template_name = 'my_app/redirect.html'
- The new template inserts the parameters into the URL and redirects to the website.
Hope this helps someone else in the future. Or if someone has a better idea, please let me know.

- 91
- 1
- 6
-
It seems to me that you are doing something wrong. I'm sorry I have no time to implement ListView and check, could you provide me with concept of what you are doing, like code that renders users in a way you do it in your app? Any if it works and satisfies you, it's a good solution 8 ). – Ilya Dec 19 '14 at 18:09
If you are limited in modifying the original view that renders the data, then you can just make a href to a specific view - from each data item that you want to redirect the user with - that calculates the new URL and then redirects the user.
In your views.py
:
from django.http import HttpResponseRedirect
def redirect_view(request, args):
new_url = figure_out_url(args)
return HttpResponseRedirect(new_url)
Then, in your template, for any item that you wish to click on to get to the redirect view, do
<a href="{% url views.redirect_view args %}">Some data item</a>
Obviously you will need to set up your urlconf
for this redirect view, but this seems like it should be doing what you want:
- User clicks on the link
- The new view determines the redirect URL based on the arguments provided
- The user is redirected to the new URL
Alternatively (and possibly a better option), you could modify the view that renders the data on the page that the user is clicking the URL, and construct the URL that you wish to redirect to (for each data item) and return it in the context somehow. e.g.
{% for item in items %}
<a href="{{ item.url }}">{{ item }}</a>
{% endfor %}
Or, write a template tag that takes data and spits out the appropriate URL to redirect to, and then just put that inside of the <a>
tag. e.g.
{% for item in items %}
<a href="{% get_redirect_url item %}">{{ item }}</a>
{% endfor %}
These approaches simply make the page a bunch of links, rather than needing any kind of specific redirect view.
Another method would be to use JavaScript, but I don't think that would be necessary in these cases.

- 324
- 2
- 5