程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django uses the check box and gets the selected value

編輯:Python

Front page

Name is get_checkbox.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
<input type="checkbox" name="my_value" value="1">&nbsp; The value is 1
<input type="checkbox" name="my_value" value="2">&nbsp; The value is 2
<input type="checkbox" name="my_value" value="3">&nbsp; The value is 3
<input type="checkbox" name="my_value" value="4">&nbsp; The value is 4
<input type="checkbox" name="my_value" value="5">&nbsp; The value is 5
<input type="submit">
{% csrf_token %}
</form>
{
{ select_value }}
</body>
</html>

back-end logic

from django.views.generic import View
class CheckBoxView(View):
def get(self, request):
return render(request, "get_checkbox.html")
def post(self, request):
value_list = request.POST.getlist("my_value", [])
return render(request, "get_checkbox.html", {

"select_value": value_list,
})

URL Connect

from django.urls import path
from .views import CheckBoxView
urlpatterns = [
path("check_box/", CheckBoxView.as_view())
]

Show examples

visit :http://127.0.0.1:8000/check_box/

You can see :

After selecting several numbers , Click on the submit :


Then you can see the results :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved