2017-07-19 10 views
-1

djangoを学んでいるときに単純なログインメカニズムを構築していますが、ビューコードが正しいです - ドキュメントから貼り付けられた)ブートストラップと私は理由を理解できません。具体的には、(ユーザー名とパスワードのための)request.POST.getの戻り値なし。ブートストラップフォームがdjango(POST)で動作しない

<!doctype html> 
 

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

答えて

2

それは簡単です。 <input>には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は、<label>が機能するために使用されています。

0

name属性をinputフィールドに追加する必要があります。

<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

+0

うーんどのようにそれは私の答えは異なる(またはそれ以上)でしょうか?さらに、あなたが引用したリンクは、初心者が 'name'属性の使い方をはっきりと言うのはあまりにも複雑です。 –

+2

彼はドキュメンテーションへのリンクを投稿しました:D @PraveenKumar – hansTheFranz

+0

@hansTheFranzああ...ああ、それは本当にここで非常に必要です...明確化のおかげで! ':D' –

関連する問題