您好,我一直在阅读此Django view not rendering after Ajax redirect,并具有以下代码,如果成功通过验证,它们将重定向到/main
,否则将发出警报。
otplogin.html
<!DOCTYPE html>
<html>
<head>
{% load static %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="Stylesheet"></link>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js" ></script>
</head>
<body>
<div id="otplogin">
<form class="form-horizontal" id="getotp" >
{% csrf_token %}
<div class="control-group">
<label class="control-label" for="username">username</label>
<div class="controls">
<input type="text" id="username" name="username" placeholder="username">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" value='auth' class="btn">Auth</button>
</div>
</div>
<div class="control-group">
<label class="control-label" for="otp">OTP</label>
<div class="controls">
<input type="otp" name="otp" id="otp" placeholder="OTP">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" value='login' class="btn">Login</button>
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$(".btn").click(function (ev) {
ev.preventDefault() // cancel form submission
if ($(this).attr("value") == "auth") {
console.log("CHECK 1");
$.ajax({
type:'POST',url:'/getotp2',data:{
username:$('#username').val(),csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
},success: function server_response(response) {
//console.log('POST1 success')
}
});
}
else if ($(this).attr("value") == "login") {
console.log("CHECK 2");
$.ajax({
type:'POST',url:'/otpcheck',otp:$('#otp').val(),success: function server_response(response) {
const r = JSON.parse(response);
console.log(r.authresult);
if (r.authresult=='success'){
window.location.href='/main'; //also tried with './main','../main',''http://127.0.0.1:8000/main',none worked
} else{
alert("Auth Failed");
}
}
});
}
});
});
</script>
</body>
</html>
views.py
def otplogin(request):
return render(request,'esearch/otplogin.html')
def getotp2(request):
logout(request)
global otp
username =otp = ''
if request.POST:
username = request.POST.get('username','')
otp = pyotp.TOTP('base32secret3232').now()
OTPEmail('You Auth',username,[],'Your auth is '+otp)
print ('POST'+username)
print ('POST'+otp)
return JsonResponse(json.dumps({'username':username,'otp':otp}),safe=False)
def otpcheck(request):
if request.POST:
otpentered=request.POST.get('otp','')
print(otpentered)
authresult='fail'
if otpentered==otp:
authresult='success'
print('POST'+authresult)
else:
print('POST'+authresult)
return JsonResponse(json.dumps({'authresult':authresult}),safe=False)
@login_required(login_url='/otplogin')
def search_index(request):
######
urls.py
urlpatterns = [
path('main',views.search_index,name='main'),path('otplogin',views.otplogin,name="otplogin"),path('getotp2',views.getotp2,name="getotp2"),path('otpcheck',views.otpcheck,name="otpcheck"),]
控制台可以正常打印所有内容,但是页面将重定向到http://localhost:8000/otplogin?next=/main
而不是想要的http://localhost:8000/main
。如果我删除了@login_required(login_url='/otplogin')
,它会成功重定向,但是我希望用户只有在/main
中获得success
后才能访问authresult
页面。因此,如何使Django视图仅作为从其他页面重定向的结果而被访问。谢谢。