繁体   English   中英

在Django中使用Ajax(1.10)

[英]Using Ajax with Django (1.10)

如果不刷新页面,我将无法通过Django(1.10)发送表单。 我已经搜索了关于Stackoverflow的所有其他相关问题,但没有成功。

这是我的ajax请求:

$(document).on('submit', 'signup-form', function(e){
      e.preventDefault();
      $.ajax({
        type: 'POST',
        url: '{% url "accounts:signup" %}',
        data: {
          firstname:$('#firstname').val(),
          lastname:$('#lastname').val(),
          email:$('#email').val(),
          password1:$('#password1').val(),
          password2:$('#password2').val(),
          username:$('#username').val(),
          csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
        },
        success:function(){
          alert('done');
        }
      })
    });

这是我的表格:

  <form id="signup-form" class="casemedic-color">
    {% csrf_token %}
    <label><b>E-mail and Username:</b></label>
    <br>
    <input class="input-red input-shadow" id="email" type="email" name="email" style="max-width:150px;" placeholder="e-mail">&nbsp;<input class="input-red input-shadow" type="username" id="username" name="username" style="max-width:150px;" placeholder="username">
    <br>
    <label><b>First name and Last name:</b></label>
    <br>
    <input class="input-red input-shadow" id="firstname" type="text" name="firstname" style="max-width:150px;" placeholder="first name">&nbsp;<input class="input-red input-shadow" type="text" id="lastname" name="lastname" style="max-width:150px;" placeholder="last name">
    <br>
    <label><b>Password:</b></label>
    <br>
    <input class="input-red input-shadow" id="password1" type="password" name="password1" placeholder="password">
    <br>
    <label><b>Confirm Password:</b></label>
    <br>
    <input class="input-red input-shadow" id="password2" type="password" name="password2" placeholder="confirm">
    <br>
    <br>
    <button class="btn casemedic-button" type="submit"><i class="fa fa-user-plus" aria-hidden="true"></i>&nbsp;Sign up</button>

    <br>
    <br>
  </form>

这是我的视图功能。 (在这种情况下,它仍应刷新页面,但它甚至无法接收刷新请求。) 此功能位于一个名为accounts的应用程序中。 (帐户:注册)

def signup(request):
    if request.method == 'POST':
        if request.POST['username'] and request.POST['password1'] and request.POST['password2'] and request.POST['firstname'] and request.POST['lastname'] and request.POST['email']:
            if request.POST['password1'] == request.POST['password2']:
                try:
                    user = User.objects.get(username=request.POST['username'])
                    return render(request, 'firstpage.html', {'error':'Username has alredy been taken'})
                except User.DoesNotExist:
                    user = User.objects.create_user(username=request.POST['username'].lower(), password=request.POST['password1'], first_name=request.POST['firstname'], last_name=request.POST['lastname'], email=request.POST['email'] )

                    user_ex = UserEx(user=user)
                    user_notifications = UserNotification(user=user)
                    user_likes = UserLike(user=user)
                    user_saves = UserSave(user=user)

                    user_ex.save()
                    user_notifications.save()
                    user_saves.save()
                    user_likes.save()

                    login(request, user)

                    return redirect('handler:home')
            else:
                return render(request, 'firstpage.html', {'error':'Passwords didn\'t match'})
        else:
            return render(request, 'firstpage.html', {'error':'Please, fill in the form properly.'})
    else:
        return render(request, 'firstpage.html')

这是网址:

url(r'^signup/', views.signup, name='signup'),

当然可以。

但是,您的输入都没有id属性,因此所有jQuery选择器都会失败。 输入输入ID,或使用其他类型的选择器(例如[name="whatever"] )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM