0

I need return redirect(url) or something like this in django, and force template to do to this url.

It returns me template html-code instead when EXACTLY redirect is required. Any ideas? Now i have to write redirect in templates window.location='url' , it works, but make code tangled.

django.__version__ == '2.0.1'

I need django text, that does like javascript window.location='myurl' Part of view

@csrf_exempt
def CheckState(request):
    ...
    try:
        ... if (condition):
        a =  redirect('/mypage/')
        ...
        return a #redirect('http://localhost:port/mypage/')

part of template (js.code)

$(document).ready(function() {
    $.get('/my/CheckState/', {})
        .success(function(data){
            console.log(data); 
//window.location = 'url' works here, otherwice no redirect! 
//In console i see html-code of 'http://localhost:port/mypage/'
            })
            .error(function(xhr, textStatus, errorThrown){
                console.log(xhr.responseText);
            })

--comment--

 a._headers = {'content-type': ('Content-Type': 'text/html; charset=utf-8'),
    'location' : ('Location', '/mypage' )}

I saw this header before i asked question, but problem exists - no jump doing. Why is this redirect not working?

Alasdair
  • 298,606
  • 55
  • 578
  • 516
Vova
  • 563
  • 8
  • 20
  • The [`redirect`](https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/#redirect) shortcut in Django lets you do `return redirect(url)` as you wish. If you have a problem using it then you'll need to include more information in your question, e.g. the URL pattern, view, and full traceback of any error message. – Alasdair Nov 14 '18 at 15:08
  • Your question is unclear. How could a redirect *not* be "exact"? And what does that code showing the Django version have to do with anything? – Daniel Roseman Nov 14 '18 at 15:08
  • @Alasdair , `return redirect(url)` returns html-code of needed of that page, but not jumping. I need to do JUMP. I need an django version of `window.location='url'` – Vova Nov 14 '18 at 15:11
  • No, `return redirect(url)` always returns a response with the `Location` header set to the new URL. Your browser or JavaScript may be following that redirect and fetching the html from the new location, but you haven't included any information about that in your question so we can't help you. – Alasdair Nov 14 '18 at 15:14
  • @Alasdair , i added code example – Vova Nov 14 '18 at 15:22

1 Answers1

0

It's not possible to prevent $.get() (which uses Xmlhttprequest) from following the redirect. See this question for an explanation.

You might be able to use the fetch API which does allow you to prevent redirects.

If you can't do that, you could change the view to return the new URL in the response,

from django.http import JsonResponse

def CheckState(request):
    return JsonResponse({'status': 'redirect', 'url': '/new/url/'}) 
   ...

Then in your ajax handler, check the response for status=redirect, and if so set window.location to the url.

Alasdair
  • 298,606
  • 55
  • 578
  • 516