在Django(1.10)中使用Ajax

2024-04-26 21:53:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如果不刷新页面,我无法通过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:

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

Tags: namebridinputemailrequesttypeusername