簡體   English   中英

DisallowedRedirect(使用協議不安全重定向到 URL)Django

[英]DisallowedRedirect (Unsafe redirect to URL with protocol) Django

當我登錄用戶時出現 DisallowedRedirect 錯誤 這兩個視圖是

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('新聞')

如果要重定向到自定義方案,除了當前答案之外,還可以使用以下代碼:

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


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

確保當您收到此錯誤時,您在 URL 前面提供了正確的方案。 默認情況下, django.http.HttpResponseRedirect不允許重定向到不以以下方案之一開頭的 URL:

  • 網址
  • https
  • FTP

因此,如果您提供的 URL 是,例如, localhost:8000 ,請確保將其更改為http://localhost:8000以使其正常工作。

不要忘記,除了啟用重定向之外,如今 Safari 不會打開重定向的深層鏈接,除非您執行此處列出的工作: https ://developer.apple.com/documentation/xcode/supporting-associated-domains

  1. 將 url 路徑添加到您的 Django 應用程序中:
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 的關聯域中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM