Django POST请求空表单HTML表单

时间:2017-05-15 03:19:13

标签: html django forms django-rest-framework

由于某些原因,当我尝试提交简单的HTML表单时,request.bodyrequest.POST都为空。

HTML是:

<div id="signup">
      <h1>Sign Up for Free</h1>

      <form action="/accounts/register" method="POST">

          <div class="top-row">
            <div class="field-wrap">
              <label>
                First Name<span class="req">*</span>
              </label>
              <input type="text" required autocomplete="off" />
            </div>

            <div class="field-wrap">
              <label>
                Last Name<span class="req">*</span>
              </label>
              <input type="text"required autocomplete="off"/>
            </div>
          </div>

          <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input type="email"required autocomplete="off"/>
          </div>

          <div class="field-wrap">
            <label>
              Set A Password<span class="req">*</span>
            </label>
            <input type="password"required autocomplete="off"/>
          </div>

          <button type="submit" class="button button-block"/>Get Started</button>

      </form>

    </div>

终点是:

@api_view(['GET', 'POST'])
def user_register(request):
    if request.method == 'GET':
        return render(request, 'authentication.html')
    elif request.method == 'POST':
        print("Register")
        print request.body
        print request.POST
    else:
        return render(request, '404.html')

1 个答案:

答案 0 :(得分:2)

<div id="signup">
      <h1>Sign Up for Free</h1>

      <form action="/accounts/register" method="POST">

          <div class="top-row">
            <div class="field-wrap">
              <label>
                First Name<span class="req">*</span>
              </label>
              <input type="text" name="first_name" required autocomplete="off" />
            </div>

            <div class="field-wrap">
              <label>
                Last Name<span class="req">*</span>
              </label>
              <input type="text" name="last_name" required autocomplete="off"/>
            </div>
          </div>

          <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input type="email" name="email" required autocomplete="off"/>
          </div>

          <div class="field-wrap">
            <label>
              Set A Password<span class="req">*</span>
            </label>
            <input type="password" name="password" required autocomplete="off"/>
          </div>

          <button type="submit" class="button button-block"/>Get Started</button>

      </form>

    </div>

在html中使用此代码,因为你没有传递名称attrribute