DisabledRedirect(使用协议重定向到URL的不安全重定向)Django



当我将用户登录时,我收到了DisabledRedirect错误这两个视图是

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})

而不是

return HttpResponseRedirect('news:home',request)

这个:

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

return redirect('news:home')

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

HttpResponseRedirect.allowed_schemes.append('news')

如果您想重定向到自定义方案,除了当前答案外,还可以使用以下代码:

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

def redirect(request):
    return CustomSchemeRedirect('tg://resolve?domain=durov')

确保当您收到此错误时,您的URL前面提供了正确的方案。默认情况下,django.http.HttpResponseRedirect不允许重定向到不以以下方案之一开头的URL:

  • http
  • https
  • ftp

因此,如果您提供的URL是,例如,localhost:8000,请确保将其更改为http://localhost:8000以使其工作。

别忘了,除了启用重定向外,现在Safari不会打开重定向的深度链接,除非你完成这里概述的工作:https://developer.apple.com/documentation/xcode/supporting-associated-domains

  1. 在Django应用程序中添加url路径:
path('.well-known/apple-app-site-association', views.web.links.appleAppSiteAssociation, name='.well-known/apple-app-site-association'),
  1. 视图应该返回一个JSON响应:

def appleAppSiteAssociation(request_):
   """
   Tell Apple that certain URL patterns can open the app
   :param request_:
   :return:
   """
   json = {
     "applinks": {
         "details": [
              {
                "appIDs": ["MY.APP.BUNDLEID"],
                "components": [
                  {
                     "#": "no_universal_links",
                     "exclude": True,
                     "comment": "Matches any URL whose fragment equals no_universal_links and instructs the system not to open it as a universal link"
                  },
                  {
                     "/": "/dataUrl=*",
                     "comment": "Matches any URL whose path starts with /dataUrl="
                  },
                ]
              }
          ]
      },
      "webcredentials": {
         "apps": ["MY.APP.BUNDLEID"]
      },
   }
   return JsonResponse(json)
  1. webcredentials:MYPROTOCOL添加到XCode中的关联域中

最新更新