2017-06-15 9 views
0

私のプログラムの目的は、入力時にデータベースに姓と名を入れ、Submitボタンをクリックして、手動でデータベースに入っているものをチェックインすると、ユーザーが何を入力したかを確認できます。提出されました。 Submitボタンをviews.pyファイルに接続して、onClick()のように並べ替えることができますが、今度は.pyというファイルに移動する方法が必要です。 (私がこの思考の列車に間違っていれば私を修正してください)。[送信]ボタンを.pyファイルにどのように接続できますか?

これを行うにはどうすればいいですか?

from django.http import HttpResponse 
from django.shortcuts import render 
from .models import Person 

def index(request): 
    if request.method == 'POST': 
     first_name = request.POST.get('firstName') 
     last_name = request.POST.get('lastName') 
     if first_name and last_name: 
      user = Person.objects.create(firstName=first_name, lastName=last_name) 
      user.save() 
    return render(request, 'music/index.html') 

def detail(request, user_id): # Testing out page 2 
    return HttpResponse("<h2>Page # (testing this out) " + str(user_id) + "</h2>") 

はここに私のindex.htmlファイルです:

は、ここに私のviews.pyファイルです

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>The Page</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <link rel="stylesheet" href="index.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 

<body> 
    <div class="container"> 
     <form action="#"> 
      <div class="form-group"> 
       <label for="firstName">First Name:</label> 
       <input type="email" class="form-control" id="firstName" placeholder="Enter first name" name="firstName"> 
      </div> 
      <div class="form-group"> 
       <label for="">Last Name:</label> 
       <input type="email" class="form-control" id="lastName" placeholder="Enter last name" name="lastName"> 
      </div> 
     </form> 
      <div class="checkbox"> 
       <label><input type="checkbox" name="remember">Remember me</label></div></br> 
       <button type="submit" class="btn btn-default">Submit</button> 
      </div> 
    </div> 
</body> 
</html> 

答えて

1
フォームは次のように宣言する必要があり

​​

とあなたの<button type="submit"><form></form>内にある必要があります。

同じ方法でGETメソッドとPOSTメソッドを扱うので、action="#"属性を削除すると、アクションは同じビューを指します。

+0

私にとっては完璧です。ありがとう – bojack

関連する問題