Django在提交时形成错误

时间:2017-05-05 08:20:08

标签: django

当我点击添加表单时,名称在数据库中排序为false

我该怎么做才能避免这个问题?

观点:

def addTeam(request) :
    team1=team()
    team1.name=request.POST['name']
    team1.logo = request.POST['logo']
    team1.save()

    return redirect("https://www.google.ps")

网址:

url(r'^add/$', views.addTeam, name='team-add')

表格作为模态::

<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;
        </button>
        <h4 class="modal-title">Add team</h4>
      </div>
      <div class="modal-body">
        <form action="" method="post">
          {% csrf_token %}
          <input type="text" name="name" id="name" />
          <label for="name">  </label>  <br>
          <input type="text" name="logo" id="logo">
          <label for="logo">  </label>  <br>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-
          dismiss="modal">Close</button>
        <a href="{% url 'team-add' %}"> <button type="button" class="btn btn-
          default" >add</button></a>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

您正尝试点击添加按钮发布表单,但您使用<a>标记发布数据。 <a>标记将始终重定向指定的网址,但不会发布数据。

因此,要发布数据,请在<form>标记中提供网址,如下所示:

<form actions="{% url 'team-add' %}">

并将<button>标记放在<form>标记

<button type="button" class="btn btn-
          default" >add</button>

并将<button>标记类型设为type="submit"

所以,代码如下所示:

<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;
            </button>
            <h4 class="modal-title">Add team</h4>
        </div>
        <div class="modal-body">
            <form action="{% url 'team-add' %}" method="post">
                {% csrf_token %}
                <input type="text" name="name" id="name" />
                <label for="name">  </label>  <br>
                <input type="text" name="logo" id="logo">
                <label for="logo">  </label>  <br>
                <button type="submit" class="btn btn-
                    default" >add</button></a>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-
                dismiss="modal">Close</button>
        </div>
    </div>
</div>
</div>