【问题标题】:Bootstrap form not working with django ( POST)引导表单不适用于 django(POST)
【发布时间】:2017-12-24 14:43:40
【问题描述】:

我在学习 django 时正在构建一个简单的登录机制,并且我的视图代码是正确的 - 它适用于一个简单的表单,但它不适用于这个(实际上是从文档中复制粘贴)引导程序,我可以不知道为什么。特别是(对于用户名和密码)request.POST.get 都没有返回。

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
	<meta http-equiv = "X-UA-Compatible" contant = "IE = edge">
	<meta name = 'viewport' content = 'width = device-width, initial-scale = 1'>
	
 <title>title</title>
  <meta name="description" content="Minimal Blogging Platform">
  <meta name="author" content="MiBlo">
	{% load staticfiles %}
  <link rel="stylesheet" href="{% static '/css/bootstrap.min.css' %}">
	<script src="{% static '/js/bootstrap.min.js' %}"></script>
</head>
	
	
	
	
<body>
	<h1 align='middle' style="margin-bottom: 80px; margin-top: 80px;">Blogging made (very) minimal</h1>
	<form action = "/blog/login/" class="form-horizontal col-sm-offset-3" method='post'>{% csrf_token %}
  		<div class="form-group">
    		<label for="username" class="col-sm-2 control-label">Username</label>
    		<div class="col-sm-3">
    			<input type="text" class="form-control" id="username" placeholder="Username" value="chuj">
    		</div>
  		</div>
  		<div class="form-group">
    		<label for="password" class="col-sm-2 control-label">Password</label>
    		<div class="col-sm-3">
      			<input type="password" class="form-control" id="password" placeholder="Password">
    		</div>
  		</div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-4">
      <div class="checkbox">
        <label>
          <input type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-4">
      <input type="submit" value="OK">
    </div>
  </div>
	<div class="form-group">
	<div class="col-sm-offset-2 col-sm-4">
		<a href="#">Register</a>
		</div>
		</div>
</form>
</body>
</html>

【问题讨论】:

    标签: html django forms twitter-bootstrap


    【解决方案1】:

    这很容易。 &lt;input&gt; 都没有 name 属性,这会将值传递给表单。

    <input type="text" class="form-control" id="username" placeholder="Username" value="chuj" name="username" />
    <input type="password" class="form-control" id="password" placeholder="Password" name="password" />
    

    解决方案:name属性添加到两者中。

    name="username" />
    name="password" />
    

    id 属性仅用于在客户端使用 JavaScript 识别它们。对于传统形式,name 属性比其他任何东西都重要。在这种情况下,您甚至不需要提供id。在这里,id 仅用于 &lt;label&gt; 工作。

    【讨论】:

      【解决方案2】:

      您必须在input 字段中添加name 属性。

         <input type="text" name='username' class="form-control" id="username" placeholder="Username" value="chuj">
        <input type="password" name='password' class="form-control" id="password" placeholder="Password">
      

      https://docs.djangoproject.com/en/1.11/topics/forms/#building-a-form

      【讨论】:

      • 嗯...与我的回答有何不同(或更好)?此外,您引用的链接对于任何初学者来说都太复杂了,无法清楚地说出name属性的用法。
      • 他发布了文档的链接:D @PraveenKumar
      • @hansTheFranz 哦哦...是的,这里真的非常需要...感谢您的澄清! :D
      • @hansTheFranz :facepalm: :sorry:
      猜你喜欢
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2014-10-28
      • 2015-11-04
      相关资源
      最近更新 更多