[英]“Unsafe redirect to URL with protocol 'content-type'” error in Django view
[英]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:
因此,如果您提供的 URL 是,例如, localhost:8000
,請確保將其更改為http://localhost:8000
以使其正常工作。
不要忘記,除了啟用重定向之外,如今 Safari 不會打開重定向的深層鏈接,除非您執行此處列出的工作: https ://developer.apple.com/documentation/xcode/supporting-associated-domains
path('.well-known/apple-app-site-association', views.web.links.appleAppSiteAssociation, name='.well-known/apple-app-site-association'),
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)
webcredentials:MYPROTOCOL
添加到 XCode 的關聯域中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.