How to redirect in django along with the request object
ajax, django
Solution
The reason it is rendered as HTML is because you have used `render_to_response` ...that's what it does :)
So instead of this:
return render_to_response("p/home.html", context_dict, context)
you should do:
return redirect('home')
where `'home'` is the name of the url to your homepage view in `urls.py`
you'll need this import at the top of your view file:
from django.shortcuts import redirect
in your home view you will have access to a request object and session data that you need
Problem
I am facing a problem regarding the redirects in django.I am using an ajax call to login and in return i am getting a json on failure and on success a template. But On Success the response is getting rendered as html instead it should redirect to home page using `render_to_response`. i tried Using `HttpResponseRedirect` i don't get a request object and hence i can't get the session parameter which is my requirement. Note: This might have some typos as i renamed some variables for confidentiality. View ``` def sign_in(request): """ :param request: data(json) """ response_data = None if request.method == 'POST': print "Request Landed" data = request.POST.get('data', None) data = json.loads(data) username = data.get("username", None) password = data.get("password", None) user = auth.authenticate(username=username, password=password) if user is not None: if user.is_active: auth.login(request, user) context = RequestContext(request) context_dict = {} request.session['username'] = username request.session['dd_id'] = str(P.objects.get(username=username).dd_id) return render_to_response("p/home.html", context_dict, context) else: response_data = json.dumps({'status': 'NOT OK', 'reason_phrase': 'Account confirmation is pending at ' 'your end.Please check your email ' 'address for confirmation link.'}) else: response_data = json.dumps({'status': 'NOT OK', 'reason_phrase': 'Invalid Credentials!'}) else: response_data = json.dumps({'status': 'NOT OK', 'reason_phrase': 'Invalid Request type!'}) return generate_response(response_data=response_data) ``` Update I used a ajax request and sent a redirect on failure for a json to show invalid details error and render_to_response in case of success.Failure works fine but on success i am seing the template that is being sent by render_to_response on the same page....How to cope with that.. Can anyone help me?