我有以下views.py文件
from django.shortcuts import render, redirect, render_to_response
from bs4 import BeautifulSoup as bs
import time
output = []
def register(request):
output = get_form_data(request)
if output == None:
output=""
if request.POST.get('cancel'):
return redirect('/login/')
if output == 'Registration Successful':
t_end = time.time() + 5 * 1
while time.time() < t_end:
a = 0
return redirect('/login/')
return render(request, 'template/register.html', {"output": output})
def get_form_data(request):
if request.POST:
if request.POST.get('register'):
reg_uname = request.POST.get('reg_una')
reg_pass = request.POST.get('password')
reg_cnfpass = request.POST.get('cnfpass')
reg_email = request.POST.get('emailid')
return "Registration Successful"
我想在返回渲染到register.html页面之前点击注册按钮然后想要5秒钟将页面重定向到登录页面时显示输出“注册成功”。
目前,如果注册成功,5秒后,我被重定向到登录页面,但由于我不在同一页面,因此无法看到“注册成功”消息。
以下是register.html文件供参考
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<link href="/static/register.css" type="text/css" rel="stylesheet">
<script type="text/javascript">
function validate()
{
var name,pass,cnfpass,email;
name = document.getElementById("reg_uname");
pass = document.getElementById('reg_pass');
cnfpass = document.getElementById('reg_cnfpass');
email = document.getElementById('reg_email');
if(name.value == '')
{
alert('Username Cannot be left blank!');
name.focus();
return false;
}
else if(name.value.length < 3){
alert('Username cannot be less than 4 characters!');
name.focus();
return false;
}
if(pass.value != '' || cnfpass.value != '')
{
if(pass.value != cnfpass.value)
{
alert('Passwords should match!');
pass.focus();
return false
}
else if(pass.value.length <8)
{
alert('Password cannot be less than 8 characters!');
pass.focus();
return false;
}
}
else
{
alert('Please fill the password fields!');
pass.focus();
return false;
}
if(email.value == '')
{
alert('Please enter your email ID!');
email.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form action="" method="post" name="reg_form">
{% csrf_token %}
<div id="main">
<div id="main-register-image"></div>
<div id="main-register-box">
<div id="main-register-box-uname">
<div>Username :</div>
<div><input type="text" id="reg_uname" name="reg_una" value="" ></div>
</div>
<div id="main-register-box-pass">
<div>Password :</div>
<div><input type="password" id="reg_pass" name="password" value="" ></div>
</div>
<div id="main-register-box-cnfpass">
<div>Confirm Password :</div>
<div><input type="password" id="reg_cnfpass" name="cnfpass" value="" ></div>
</div>
<div id="main-register-box-email">
<div>Email ID :</div>
<div><input type="email" id="reg_email" name="emailid" value="" ></div>
</div>
<div id="main-register-box-buttons">
<div><input type="submit" id="reg" value="Resister" name="register" onclick="return validate()" /></div>
<div><input type="submit" id="cancel" name="canc" value="Cancel" onclick="/login/" /></div>
</div>
<div id="result">{{output}}</div>
</div>
</div>
</form>
</body>
</html>
我的问题是,我们如何在html页面上显示动态数据并继续在同一页面上执行其他操作。
答案 0 :(得分:2)
您必须异步执行请求才能继续在同一页面上(AJAX)。这是一个相当简单的程序,这里的guide for Django可能会帮助您入门。
基本上,您将使用JavaScript发送您的请求并处理响应。您可以根据请求是否成功发送不同的消息,并通知用户。
此外,5秒间隔是否有任何特定目的?您正忙着在那里等待(如有必要,请使用sleep,或者使用setTimeout)将其移至等待前端。
答案 1 :(得分:1)
因此,您希望在重定向之前显示消息。您至少有两种方法:
关于观点的一些事情:
is
if output is None
进行空检查,而不是==
。POST
获取它们。也许我们无法看到代码。output
,您在output
方法中也有变量register
。考虑不要重复使用相同的变量名来防止混乱的情况。sleep
:
import time
time.sleep(t)
其中t
是睡觉的秒数。render
放在第一个if语句中:如下:
if output == None:
return render(request, 'template/register.html', {"output": ''})
最终结果如下:
from django.shortcuts import render, redirect, render_to_response
from bs4 import BeautifulSoup as bs
import time
output = []
def register(request):
message = ''
if request.method == 'POST'
if request.POST.get('cancel'):
return redirect('/login/')
# Remember, get_form_data returns boolean now.
reg = get_form_data(request)
if reg:
return redirect('/login/')
else:
output = 'Registration failed'
return render(request, 'template/register.html', {"output": message})
def get_form_data(request):
if request.POST.get('register'):
reg_uname = request.POST.get('reg_una')
reg_pass = request.POST.get('password')
reg_cnfpass = request.POST.get('cnfpass')
reg_email = request.POST.get('emailid')
# Some registration function is probably called here
# Perhaps return a boolean instead, if registration was OK:
return register(username=reg_uname, pass=reg_pass, cnfpass=reg_cnfpass, email=reg_email)
return False
使用jQuery,AJAX调用可能看起来像下面的代码。如果您考虑使用AJAX方法,我建议您更改注册函数以返回某种JSON响应和适当的响应状态代码。如果您打算使用更多这类电话,请查看djangorestframework。
function register() {
data = {};
// Get the data from the form fields here: e.g.:
data['username'] = $('reg_uname').val();
$.ajax({
url: '/register',
method: 'POST',
data: data,
success: function(data, textStatus, xhr) {
// Handle success scenario here.
},
error: function(xhr, status, error) {
// Handle fail scenario here.
}
)};
}
答案 2 :(得分:0)
如上所述,你需要使用AJAX,或者你可以只返回一个中间人&#34;成功&#34;使用Javascript将用户重定向到登录页面的页面。我想说这是最简单的方法:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p>Success!</p>
<script type="text/javascript">
window.onload = function() {
setTimeout(function(){
window.location = '/login'; // redirects to login page
}, 10000); // waits 10 seconds
};
</script>
</body>
</html>
&#13;