Javascript i';我正在编写注册表html,django+;ajax,部分加载页面,但内存增加,页面被卡住
django veiewsJavascript i';我正在编写注册表html,django+;ajax,部分加载页面,但内存增加,页面被卡住,javascript,django,Javascript,Django,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']
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>
我的意图是直接将返回的结果加载到“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没有提交数据。我在服务器请求日志中没有看到该消息,它一直在创建新的变量,导致浏览器内存增加,页面卡住。但我还没找到发生的地方。
希望你的答案能解决问题。thk我单独写了一个测试,发现是验证码的问题。如果你没有验证码,你可以正常提交并在本地加载,但是如果你有验证码,你会得到一个错误。是的,现在可能真的是验证码的问题。当我删除验证码时,它工作正常。但是当我添加它时,服务器的日志将报告一个错误并记录'Django'。乌提尔斯。数据结构。多值DictKeyError:“hashkey”。因此,我认为前端获取“hashkey”值的方法是错误的,但我还没有找到获取“hashkey”值的方法。
$.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;