0

I'm new to django and this is giving me a problem. I'm trying to get a simple GET variable. mysite/search/?q=&start=10&end=20&language=Python it works..

My view is:

def search(request, invalid_subscribe_to_alert_form=None):
    ...
    start = int(request.GET.get('start', 1)) 
    end = int(request.GET.get('end', 10))

and then I request this

mysite/search/?q=&start=aword&end=20&language=Python

How do I redirect to the same page in after catching the value error?such that request is gone back to 404 page.

rrindam
  • 47
  • 2
  • 8

2 Answers2

0

Use the Django session framework. In the previous correct views, you have to store the value of the last correct page visited:

def search(request, invalid_subscribe_to_alert_form=None):
   # ...
   request.session['last-correct-path'] = request.path

Then later in the failing view:

def search(request, invalid_subscribe_to_alert_form=None):
       # ...
       except ValueError:
           return HttpResponseRedirect(request.session['last-correct-path'])

Anyhow if I were you I'd better return 404 or display some error message instead of just redirecting to the last correct page.

dukebody
  • 7,025
  • 3
  • 36
  • 61
  • Well I am new to django and it will be helpful if you can tell how can i redirect to a 404 page. – rrindam Nov 01 '14 at 19:32
  • Google is your friend. But anyhow https://docs.djangoproject.com/en/dev/topics/http/views/#the-http404-exception – dukebody Nov 02 '14 at 18:53
0

Magic is worse than predictability. This SO has multiple answers pointing towards 40X being the right status code to return when users muck up the parameters in an URL.

Also: Django has pagination support (search plugins and REST frameworks often do this for you too, DRF for example).

Community
  • 1
  • 1