javascript - 我正在写注册 html,django + ajax,部分加载页面,但是内存增加并且页面卡住了
问题描述
django 视图
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 没有提交数据。我在服务器请求日志中没有看到消息,一直在创建新变量,导致浏览器内存增加,页面卡住。但我还没有找到它发生的那一行。希望你的回答能解决问题。thk
解决方案
推荐阅读
- python - 在电子邮件链接中传递给 url_for 的变量不起作用
- android - Xamarin - 使用 android 原生库
- influxdb - 更改默认保留策略长度
- java - 服务器到客户端通信客户端无法从服务器接收消息
- vba - 如何在我的 for/next 循环中添加这些值和文本字符串?
- html - 如何在每个具有css文本的td元素上放置一个小方框?
- python - 使用 nidaqmx 进行连续采集
- pagekite - 是否可以在多台机器上扩展 pagekite?
- apache-kafka - 如何获取数据:max.poll.records 和偏移量
- docker - Kubernetes 没有从私有注册表中提取图像