weixin_33716557 2017-06-20 16:03 采纳率: 0%
浏览 26

在Django(1.10)中使用Ajax

I couldn't send a form through django (1.10) without refreshing the page. I have searched every other related question on Stackoverflow but no success.

This is my ajax request:

$(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');
        }
      })
    });

This is my form:

  <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>

This is my view function. (In this case, it should still refresh the page, but it cannot even receive the request to refresh.) This function is inside an app called accounts. (accounts:signup)

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')

This is the url:

url(r'^signup/', views.signup, name='signup'),
  • 写回答

1条回答 默认 最新

  • weixin_33743248 2017-06-20 16:07
    关注

    Of course it's possible.

    However none of your inputs have id attributes, so all your jQuery selectors will fail. Either give the inputs IDs, or use another type of selector (eg [name="whatever"]).

    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程