为什么以下代码不更新数据库?

时间:2016-12-31 12:29:12

标签: jquery ajax django

我正在尝试使用带有ajax代码的bootstrap模式表单来更新我的数据库的内容。但是我的数据库没有使用表单中提供的输入进行更新。表单工作得很好,但我没有看到数据库中的内容更新。怎么了 ???请帮助,因为我搜索整个互联网,什么都没有:(

HTML代码:

<div class="container">
    <h1> {{ username }} </h1>
  <h2>Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
  <form id="new_user_form">
      {% csrf_token %}
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <div class="form-group"  >
              <label for="usr">Username:</label>
              <input type="text" name="username" class="form-control input-lg" id="username">
              <label for="passwordd">Password:</label>
              <input type="password" name="password" class="form-control input-sm" id="password">
          </div>
        </div>
        <div class="modal-footer">
          <button type="submit" class="btn btn-default" id="submit">Submit</button>
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>
  </form>
  <script type="text/javascript" src="/static/bootstrap 3/js/bootstrap.min.js"></script>

Ajax代码:

<script>
 $(document).on('submit','#new_user_form',function(e){
    e.preventDefault();
    $.ajax({
            type: "POST",
            url: "/testapp/registered/",
            data: {
                    username: $('#username').val(),
                    password: $('#password').val(),
                    csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
            },
            sucess: function(){
                alert("Registeration successfully.")
            }
            error: function(jqXHR, textStatus, errorThrown)
            {
                alert(textStatus, errorThrown);
            }
    });
});


</script>

views.py:

from django.shortcuts import render
from testapp.models import User

def index(request):
    template = 'index.html'
    return render(request,template)

def create_user(request):
    if request.method == 'POST':
        User.objects.create(
            username = request.POST['username'],
            password = request.POST['password'],
        )
        User.save()
    return render(request,'testapp/index.html',{'username':request.POST['username']})

testapp \ urls.py:

from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^$', views.index, name="Index"),
    url(r'^registered/$', views.create_user, name="registered"),
]

2 个答案:

答案 0 :(得分:0)

只做这个: my_user = User.objects.create(用户名= request.POST [&#39;用户名&#39;],密码= request.POST [&#39;密码&#39;]) my_user.save() 您需要保存已创建的模型实例,而不仅仅是调用模型的.save()函数。

答案 1 :(得分:-1)

在testapp \ urls.py中:交换网址路由逻辑

from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^registered/$', views.create_user, name="registered"),
    url(r'^$', views.index, name="Index")
]