Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisallowedRedirect (Unsafe redirect to URL with protocol) Django

I am getting DisallowedRedirect error when i am logging user in The two views are

def login(request):
    c={}
    c.update(csrf(request))
    form=LoginForm()
    errors=()
    c['form']=form
    c['errors']=errors
    return render(request,'news/login.html',c)

def auth_view(request):
    username=request.POST.get('username','')
    password=request.POST.get('password','')
    user=auth.authenticate(username=username,password=password)
    if user is not None:
        auth.login(request,user)
        return HttpResponseRedirect('news:home',request)
    else:
        form=LoginForm()
        errors=('Invalid Username or Password',)
        return render(request,'news/login.html', {'form':form,'errors':errors})
like image 682
Abhishek Rastogi Avatar asked Dec 25 '15 19:12

Abhishek Rastogi


4 Answers

In addition to the current answers if you want to redirect to an custom scheme, you can use following code:

class CustomSchemeRedirect(HttpResponsePermanentRedirect):
    allowed_schemes = ['tg']


def redirect(request):
    return CustomSchemeRedirect('tg://resolve?domain=durov')
like image 97
Andrei Avatar answered Nov 19 '22 02:11

Andrei


instead of

return HttpResponseRedirect('news:home',request)

this:

return HttpResponseRedirect(reverse('news:home'))

or

return redirect('news:home')

or

return redirect(reverse('news:home'))
like image 41
doniyor Avatar answered Nov 19 '22 00:11

doniyor


HttpResponseRedirect.allowed_schemes.append('news')

like image 26
Bogle Shun Avatar answered Nov 19 '22 02:11

Bogle Shun


Make sure that when you get this error you have the correct scheme supplied in front of your URL. By default the django.http.HttpResponseRedirect does not allow redirects to URLs that don't start with one of the following schemes:

  • http
  • https
  • ftp

So if the URL you supply is, for example, localhost:8000 make sure you change it to http://localhost:8000 to get it to work.

like image 2
Bono Avatar answered Nov 19 '22 00:11

Bono