我如何处理 render_to_response 和 ajax,以便可以使用内容重定向页面?
How can i handle render_to_response with ajax, so that the page could be redirected with the content?
views.py
def usersignup(request):
content = {}
userinfo = user_signup.objects.all()
if request.method == "POST":
fullname = request.POST.get('fullname')
email = request.POST.get('email')
password = request.POST.get('password')
for i in userinfo:
if i.user_email == email:
# content['err_msg'] = 'Email Id already exists'
return HttpResponse('Already Exist')
# return render_to_response("index.html",content,context_instance=RequestContext(request))
userobj = user_signup.objects.create(full_name=fullname, user_email=email, user_password=password)
content['fullname'] = fullname
content['useremail'] = email
request.session['user'] = userobj.user_email
return render_to_response("users/user_signup.html",content,context_instance=RequestContext(request))
index.js
function usersignup()
{
var csrftoken = getCookie('csrftoken');
emailid = document.getElementById('nuemail').value
password = document.getElementById('nupassword').value
fullname = document.getElementById('nufullname').value
alert(emailid);
alert(password);
$.ajax({
url: '/usersignup/',
type: 'POST',
data : {
// CSRF : csrftoken,
email : emailid,
password : password,
fullname : fullname
},
}).done(function(response){
alert(response);
if (response == "Already Exist")
{
$('#p2').empty();
alert(response);
$('#p2').append('EmailId already exists');
}
});
}
index.html
<div class="md-modal " id="modal-19"> <!-- add flip efffect -->
<div class="md-content modal-form" id="usersignup">
<h3>Sign Up <img alt="" class="md-close right" onclick="closeAll()" src="/static/img/close_pink.png" ></h3>
<div>
<section class="modal-inside">
<div class="row">
<div class="columns large-12">
<div class="group">
<input type="text" name="fullname" id= "nufullname" required> <span class="highlight"></span>
<span class="bar"></span> <label>Full name</label>
</div>
</div>
</div>
<div class="row">
<div class="columns large-12">
<div class="group">
<input type="email" name="email" title="In this format 'aaaaaa@bbbb.ccc'"
id = "nuemail" required> <span
class="highlight"></span> <span class="bar"></span> <label>Email
address</label>
</div>
</div>
</div>
<div class="row">
<div class="columns large-12">
<div class="group">
<input type="password" name='password' id = "nupassword" required> <span class="highlight"></span>
<span class="bar"></span> <label>Password</label>
</div>
</div>
</div>
<div class="row">
<div class="columns large-8">
<h6>
Have an account? <a class=" md-close md-trigger"
onclick="userLogIn()" data-modal="user-sign-in"> Log in</a>
</h6>
</div>
<div class="columns large-4">
<button class="tiny round right" onclick="usersignup();">SIGN UP</button>
</div>
<p id="p2"></p>
</div>
</section>
</div>
</div>
我已经添加了三个文件,所以我的问题是每当我创建一个新用户时,我希望页面应该重定向到我使用 render_to_response.[= 的 ("users/user_signup.html") 18=]
我也使用了 ajax 函数,在 index.js 中我只是想检查响应,如果电子邮件 ID 已经存在,它应该 return Httpresponse 作为( "Already Exist") 它工作正常。
代码的问题在于,如果 render_to_response 页面没有重定向到其他页面,而是 return 响应 ajax 函数。有什么解决办法,我怎样才能简单地重定向页面。请帮我TIA。
我正在调用 render_to_response 使用 post 方法,在该方法上我得到了整个 HTML 页面的响应,所以我的解决方案只是使用加载页面的 get 方法调用 render_to_response。
views.py
def usersignup(request):
content = {}
userinfo = user_signup.objects.all()
if request.method == "POST":
fullname = request.POST.get('fullname')
email = request.POST.get('email')
password = request.POST.get('password')
for i in userinfo:
if i.user_email == email:
# content['err_msg'] = 'Email Id already exists'
return HttpResponse('Already Exist')
# return render_to_response("index.html",content,context_instance=RequestContext(request))
userobj = user_signup.objects.create(full_name=fullname, user_email=email, user_password=password)
content['fullname'] = fullname
content['useremail'] = email
request.session['user'] = userobj.user_email
return render_to_response("users/user_signup.html",content,context_instance=RequestContext(request))
index.js
function usersignup()
{
var csrftoken = getCookie('csrftoken');
emailid = document.getElementById('nuemail').value
password = document.getElementById('nupassword').value
fullname = document.getElementById('nufullname').value
alert(emailid);
alert(password);
$.ajax({
url: '/usersignup/',
type: 'POST',
data : {
// CSRF : csrftoken,
email : emailid,
password : password,
fullname : fullname
},
}).done(function(response){
alert(response);
if (response == "Already Exist")
{
$('#p2').empty();
alert(response);
$('#p2').append('EmailId already exists');
}
});
}
index.html
<div class="md-modal " id="modal-19"> <!-- add flip efffect -->
<div class="md-content modal-form" id="usersignup">
<h3>Sign Up <img alt="" class="md-close right" onclick="closeAll()" src="/static/img/close_pink.png" ></h3>
<div>
<section class="modal-inside">
<div class="row">
<div class="columns large-12">
<div class="group">
<input type="text" name="fullname" id= "nufullname" required> <span class="highlight"></span>
<span class="bar"></span> <label>Full name</label>
</div>
</div>
</div>
<div class="row">
<div class="columns large-12">
<div class="group">
<input type="email" name="email" title="In this format 'aaaaaa@bbbb.ccc'"
id = "nuemail" required> <span
class="highlight"></span> <span class="bar"></span> <label>Email
address</label>
</div>
</div>
</div>
<div class="row">
<div class="columns large-12">
<div class="group">
<input type="password" name='password' id = "nupassword" required> <span class="highlight"></span>
<span class="bar"></span> <label>Password</label>
</div>
</div>
</div>
<div class="row">
<div class="columns large-8">
<h6>
Have an account? <a class=" md-close md-trigger"
onclick="userLogIn()" data-modal="user-sign-in"> Log in</a>
</h6>
</div>
<div class="columns large-4">
<button class="tiny round right" onclick="usersignup();">SIGN UP</button>
</div>
<p id="p2"></p>
</div>
</section>
</div>
</div>
我已经添加了三个文件,所以我的问题是每当我创建一个新用户时,我希望页面应该重定向到我使用 render_to_response.[= 的 ("users/user_signup.html") 18=]
我也使用了 ajax 函数,在 index.js 中我只是想检查响应,如果电子邮件 ID 已经存在,它应该 return Httpresponse 作为( "Already Exist") 它工作正常。
代码的问题在于,如果 render_to_response 页面没有重定向到其他页面,而是 return 响应 ajax 函数。有什么解决办法,我怎样才能简单地重定向页面。请帮我TIA。
我正在调用 render_to_response 使用 post 方法,在该方法上我得到了整个 HTML 页面的响应,所以我的解决方案只是使用加载页面的 get 方法调用 render_to_response。