0

I'm using Django but being crazy and submitting forms with Javascript and stuff.

I have a form that has an action that's only there because I couldn't get it to process the data I wanted correctly (that is, I'm using the action URL to redirect to two different parts of a view that each do what I want). It's working great, except after using the forms I see the URL www.example.com/formone and www.example.com/formtwo after pressing each. Due to the way it's setup it looks just like www.example.com, except for the URL. Also if you reload the page without the POST data, using the same URL, it fails, because I only process post data from those URLs.

Is there a way to redirect at the end of the view?

For example, something like:

def formone(request):
...
...
return render(request, 'example.html', 'www.example.com', {
         'formone': form, 'formtwo': voteform, 'items': items
            })

where www.example.com is the redirect URL, would be desirable. I can probably do this in Javascript on the actual page, but that seems even more sketchy.

rofls
  • 4,993
  • 3
  • 27
  • 37
  • I am not sure if I am following. It would probably be better if you can provide the relevant parts from the urls.py and views.py. – tamakisquare Oct 30 '12 at 06:47
  • 1
    Can you just use [the redirect shortcut](https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect) at the end of your view? – tamakisquare Oct 30 '12 at 06:48
  • Ok, you should post that as an answer though! I'm trying the above solution, will add more details if it doesn't work. – rofls Oct 30 '12 at 06:50
  • I wasn't sure if I understood the question, so put it in as a comment instand of answer. – tamakisquare Oct 30 '12 at 06:53

2 Answers2

2

Use the redirect shortcut at the end of your view.

tamakisquare
  • 16,659
  • 26
  • 88
  • 129
  • Yeah, I think this should work fine (sorry SE community for not researching more first). For some reason my view is saying "Import by filename not supported" though, and interpreting my URL as a filepath... shouldn't be too hard to fix. – rofls Oct 30 '12 at 06:57
0

I actually used HTTPResponseRedirect, because the redirect shortcut wouldn't accept a URL and was expecting a filepath for some reason.

Evidently there are a few ways to do this (tsk, tsk Django, not following the Python language principle so well). Haha, I'm happy something's working though!

Community
  • 1
  • 1
rofls
  • 4,993
  • 3
  • 27
  • 37
  • [django.shortcuts.redirect source code](https://github.com/django/django/blob/1.4.1/django/shortcuts/__init__.py#L47) indicates that it works with a url as argument. "tsk, tsk Django, not following the Python language principle so well" could you elaborate ? – jpic Oct 30 '12 at 08:12
  • Apparently there are a few options for redirecting from within a view, is all I'm saying; namely `rediret`, `HTTPResponseRedirect`, and possibly some methods involving "generic views", which I don't quite understand, as they appear to be very similar to what's in the url.py file. I love Python, and Django! – rofls Oct 30 '12 at 08:33
  • Whoops, submitted that early, but `redirect` and `HTTPResponseRedirect` both take a filepath or a URL, so it seems repetitive, and I thought one of the principles of Python is that there should be only one way to do something in particular, and furthermore it should be obvious. With `response` it is obvious. – rofls Oct 30 '12 at 08:35
  • I'm sorry, I've been using Django for years and I don't understand what you are talking about. I have never had to repeat a URL **ever** in Django. Maybe there's something wrong with your explanations ? Also, did you read the source code of `django.shortcuts.redirect` that I linked in my previous comment ? You can pass a model, url name, or a url string - that should have been reversed manually with `django.core.urlresolvers.reverse`, and it will make an `django.http.HTTPResponseRedirect` or `django.http.HTTPResponsePermanentRedirect` that the view can return. I wonder what "filepath" is. – jpic Oct 30 '12 at 08:56
  • ok, sorry for bad-mouthing Django, I'm a newb; I was referring to this part of "the zen of python" [There should be one-- and preferably only one --obvious way to do it.](http://www.python.org/dev/peps/pep-0020/) And that there are a few different ways of redirecting. I used `HTTPResponeRedirect`. Evidently using simply `redirect` works for most people (or at least you two). For me, it did not. I wish I knew why it was talking about a file path or filepath or whatever too, but I don't, I simply know that `redirect` did not work for me, and the above solution did. So I'm happy. – rofls Oct 31 '12 at 05:28
  • Well obviously, django.shortcuts.redirect is a shortcut to create an HttpResponseRedirect - or HttpResponsePermanentRedirect. The zen of Python doesn't have anything against django.shortcuts, which are mainly factories. – jpic Oct 31 '12 at 09:50