django veiews
def regist(request):
hashkey = CaptchaStore.generate_key()
image_url = captcha_image_url(hashkey)
captcha = {'image_url': image_url, 'hashkey':hashkey}
if request.POST:
username = request.POST['username']
password = request.POST['password']
password1 = request.POST['password1']
key = request.POST['hashkey']
capt = request.POST['captcha']
if username != '' and password != '' and password1 != '':
if len(password) >= 6:
if captchautil.is_valid(capt, key):
check_user = User.objects.filter(username=username)
if not check_user:
if password == password1:
user = User.objects.create_user(username=username, password=password)
userprofile = UserProfile.objects.create(user=user, name=username, level='Bronze')
msg = '注册成功,现在去登录吧'
else:
msg = '密码不一致,请重新输入'
else:
msg = '用户名已存在,请重新输入'
else:
msg = '请输入正确的验证码'
else:
msg = '密码长度至少6位'
else:
msg = '请输入用户名与密码'
return HttpResponse(msg)
return render(request, 'regist.html', locals())
html代码
<form action="/regist/" method="post" id="myForm">
{% csrf_token %}
<input type="text" id='username' name="username">
<input type="password" id='password' name="password">
<input type="password" id='password1' name="password1">
<input id='captcha' name="captcha" required>
<input value="{{hashkey}}" type="hidden" name="hashkey" id='hashkey'>
<button type="submit" name="click" id='click'>注册</button>
脚本代码:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "{% url 'regist' %}",
data: {
"username": username,
"password": password,
"password1": password1,
"key": hashkey,
"capt": captcha,
"csrfmiddlewaretoken": '{{ csrf_token }}',
},
dataType: "text",
success:function(response){
alert(response);
$("#myForm").submit();
},
});
我的意图是将返回的结果直接加载到'div'中,而不加载所有html。 但是当我提交它时,整个页面直接卡住了,浏览器的内存也增加了。只有关闭页面后,我才能恢复正常。 我尝试用ajax返回false,但是它会再次发生 html代码:
$.ajax({
type: "POST",
url: "{% url 'regist' %}",
data: {
"username": username,
"password": password,
"password1": password1,
"key": hashkey,
"capt": captcha,
"csrfmiddlewaretoken": '{{ csrf_token }}',
},
dataType: "text",
success:function(response){
alert(response);
$("#myForm").submit();
},
});
return false;
我认为这样做的原因是Ajax没有提交数据。我没有在服务器请求日志中看到该消息,它一直在创建新变量,从而导致浏览器内存增加并且页面被卡住。但是我还没有找到发生这种情况的路线。 希望你的回答能解决问题。thp