首页 > 解决方案 > 我正在写注册 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

标签: javascriptdjango

解决方案


推荐阅读