Đã hỏi 7 năm, 8 tháng trước 7 years, 8 months ago
Đã xem 46k lần 46k times
Xin chào mọi người, tôi có biểu mẫu HTML như sau:
<body class=""> <div class="navbar navbar-static-top navbar-inverse"></div> <div style="height:20%; width:30%; margin-top:12%; margin-left:40%;"> <div class="list-group"> <form role="form"> <br/> <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-circle-o-notch" ></i></span> <input type="text" name="Username" class="form-control" placeholder="Username" /> </div> <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-tag" ></i></span> <input type="text" name="first_name" class="form-control" placeholder="FIRST_NAME" /> </div> <div class="form-group input-group"> <span class="input-group-addon"><i class="fa fa-tag" ></i></span> <input type="text" name="last_name" class="form-control" placeholder="LAST_NAME" /> </div> ... ... <a href="/submit" class="btn btn-success ">POST</a> </form> </div> </div> </body>Và sau khi nhấp vào bài đăng, tôi đang chuyển hướng nó sang chế độ xem.py. Bất kỳ ai cũng có thể cho tôi biết làm thế nào để lấy các giá trị trường của tất cả các trường của biểu mẫu thành chế độ xem.py. cảm ơn trước
Đã hỏi ngày 24 tháng 3 năm 2015 lúc 9:08Mar 24, 2015 at 9:08
Mỗi đầu vào phải có thuộc tính tên:
<input type="text" name="username" class="form-control" placeholder="Desired Username" />Và sau đó trong chế độ xem của bạn, bạn có thể truy cập dữ liệu đó bằng các đối tượng giống như từ điển request.POST hoặc request.GET (nó phụ thuộc vào thuộc tính method của thẻ <form>):
def register(request): username = request.POST['username'] ...Nhưng bạn không nên kết xuất/xử lý các hình thức theo cách PHPISH này. Tìm hiểu các hình thức Django.
Đã trả lời ngày 24 tháng 3 năm 2015 lúc 9:14Mar 24, 2015 at 9:14
Catavarancatavarancatavaran
43.7K8 Huy hiệu vàng93 Huy hiệu bạc84 Huy hiệu đồng8 gold badges93 silver badges84 bronze badges
8
Để bắt dữ liệu trong phụ trợ Python
Đối với yêu cầu bài viết:
def register(request): username = request.POST.get('username')Để nhận được yêu cầu
def register(request): username = request.GET.get('username').get đã mất tích trong câu trả lời trước đó
Đã trả lời ngày 20 tháng 4 năm 2018 lúc 13:23Apr 20, 2018 at 13:23
CryptoktmcryptoktmcryptoKTM
2.35521 Huy hiệu bạc19 Huy hiệu đồng21 silver badges19 bronze badges
Django thường được gọi là khung bao gồm pin bao gồm các khung hình vì nó có cài đặt mặc định cho mọi thứ và có các tính năng có thể giúp bất cứ ai phát triển trang web nhanh chóng. Nói về các biểu mẫu, trong HTML, một biểu mẫu là một tập hợp các yếu tố bên trong, cho phép khách truy cập làm những việc như nhập văn bản, chọn tùy chọn, thao tác đối tượng hoặc điều khiển, v.v., sau đó gửi thông tin đó trở lại máy chủ. Về cơ bản, nó là một tập hợp dữ liệu để xử lý nó cho bất kỳ mục đích nào bao gồm lưu nó trong cơ sở dữ liệu hoặc tìm nạp dữ liệu từ cơ sở dữ liệu. Django hỗ trợ tất cả các loại hình thức HTML và kết xuất dữ liệu từ chúng để xem xử lý bằng các hoạt động logic khác nhau.
Để biết thêm về các hình thức HTML, hãy truy cập HTML | Thẻ biểu mẫu.
Django cũng cung cấp một tính năng tích hợp của các hình thức Django giống như các mô hình Django. Người ta có thể tạo các biểu mẫu trong Django và sử dụng chúng để tìm nạp dữ liệu từ người dùng một cách thuận tiện. Để bắt đầu với các biểu mẫu, người ta cần phải làm quen với các yêu cầu GET và đăng theo biểu mẫu.Django Forms just like
Django Models. One can create forms in Django and use them to fetch data from the user in a convenient manner.
To begin with forms, one needs to be familiar with GET and POST requests in forms.
- Nhận: Ngược lại, nhận được các gói dữ liệu được gửi thành một chuỗi và sử dụng điều này để soạn một URL. URL chứa địa chỉ nơi dữ liệu phải được gửi, cũng như các khóa và giá trị dữ liệu. Bạn có thể thấy điều này trong hành động nếu bạn thực hiện tìm kiếm trong tài liệu Django, sẽ tạo ra một URL của biểu mẫu //docs.djangoproject.com/search/?q=forms&release=1.GET, by contrast, bundles the submitted data into a string, and uses this to compose a URL. The URL contains the address where the data must be sent, as well as the data keys and values. You can see this in action if you do a search in the Django documentation, which will produce a URL of the form //docs.djangoproject.com/search/?q=forms&release=1.
- Bài đăng: Bất kỳ yêu cầu nào có thể được sử dụng để thay đổi trạng thái của hệ thống - ví dụ: yêu cầu thực hiện các thay đổi trong cơ sở dữ liệu - nên sử dụng bài đăng. Any request that could be used to change the state of the system – for example, a request that makes changes in the database – should use POST.
Render HTML Forms trong Django giải thích
Minh họa các hình thức django bằng một ví dụ. Hãy xem xét một dự án có tên GeekSforGeeks có một ứng dụng có tên Geek.Django Forms using an Example. Consider a project named geeksforgeeks having an app named geeks.
Tham khảo các bài viết sau để kiểm tra cách tạo dự án và một ứng dụng trong Django.
- Làm thế nào để tạo một dự án cơ bản bằng MVT trong Django?
- Làm thế nào để tạo một ứng dụng trong Django?
Hãy để tạo một biểu mẫu HTML đơn giản để hiển thị cách bạn có thể nhập dữ liệu từ người dùng và sử dụng nó trong chế độ xem của bạn. Nhập mã sau trong Geek> Mẫu> Trang chủ.html
HTML
<<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 0 <input type="text" name="username" class="form-control" placeholder="Desired Username" /> 1 <input type="text" name="username" class="form-control" placeholder="Desired Username" /> 2<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 3 method
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<def register(request): username = request.POST['username'] ... 0 def register(request): username = request.POST['username'] ... 1def register(request): username = request.POST['username'] ... 2223def register(request): username = request.POST['username'] ... 4____________<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 7
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<def register(request): username = request.POST['username'] ... 9 def register(request): username = request.POST.get('username') 0def register(request): username = request.POST['username'] ... 223
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<def register(request): username = request.POST['username'] ... 9 def register(request): username = request.POST.get('username') 3def register(request): username = request.POST['username'] ... 2def register(request): username = request.GET.get('username') 5 def register(request): username = request.GET.get('username') 6def register(request): username = request.POST['username'] ... 2def register(request): username = request.GET.get('username') 8<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 7
request.POST0<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 0<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 7
Bây giờ để hiển thị nó trong quan điểm của chúng tôi, chúng tôi cần sửa đổi urls.py cho geek app.
Enter the following code in geeksforgeeks > urls.py
Python3
request.POST3 request.POST4request.POST5 request.POST6
request.POST3 request.POST8request.POST5 request.GET0
request.GET1def register(request): username = request.POST['username'] ... 2 request.GET3
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8request.GET5
request.GET6
Bây giờ, hãy để di chuyển đến Home_View của chúng tôi và bắt đầu kiểm tra làm thế nào chúng ta sẽ lấy dữ liệu. Toàn bộ dữ liệu từ biểu mẫu HTML trong Django được chuyển dưới dạng đối tượng JSON được gọi là yêu cầu. Trước tiên, hãy tạo một chế độ xem và sau đó chúng tôi sẽ thử tất cả các phương thức để tìm nạp dữ liệu từ biểu mẫu.
Python3
request.POST3 request.GET8request.POST5 method0
method1 method2
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8method4 method5method6method7
Vì chúng tôi có mọi thứ được thiết lập, hãy để chúng tôi chạy máy chủ chạy Python Manage.py và kiểm tra xem biểu mẫu có trên trang chủ không.
Theo mặc định, mọi biểu mẫu từng được viết bằng HTML đều thực hiện yêu cầu nhận ở phần cuối của ứng dụng, yêu cầu GET thường hoạt động bằng cách sử dụng các truy vấn trong URL. Hãy để cho thấy nó bằng cách sử dụng biểu mẫu trên, điền vào biểu mẫu bằng tên của bạn và hãy để kiểm tra những gì xảy ra.
URL trên được thêm vào thuộc tính tên của thẻ đầu vào và tên được nhập vào biểu mẫu. Đây là cách yêu cầu GET hoạt động bất cứ số lượng đầu vào nào họ sẽ được thêm vào URL để gửi dữ liệu đến phần cuối của ứng dụng. Hãy để kiểm tra cách cuối cùng để có được dữ liệu này trong chế độ xem của chúng tôi để logic có thể được áp dụng dựa trên đầu vào. & Nbsp; trong chế độ xem.pypyname attribute of the input tag and the name entered in the form. This is how the GET request works whatever be the number of inputs they would be appended to the URL to send the data to the back end of an application. Let’s check how to finally get this data in our view so that
logic could be applied based on input.
In views.py
Python3
request.POST3 request.GET8request.POST5 method0
method1 method2
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<form>5<form>6
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8method4 method5method6method7
Vì chúng tôi có mọi thứ được thiết lập, hãy để chúng tôi chạy máy chủ chạy Python Manage.py và kiểm tra xem biểu mẫu có trên trang chủ không.
Theo mặc định, mọi biểu mẫu từng được viết bằng HTML đều thực hiện yêu cầu nhận ở phần cuối của ứng dụng, yêu cầu GET thường hoạt động bằng cách sử dụng các truy vấn trong URL. Hãy để cho thấy nó bằng cách sử dụng biểu mẫu trên, điền vào biểu mẫu bằng tên của bạn và hãy để kiểm tra những gì xảy ra. returns a query dictionary that one can access like any other python dictionary and finally use its data for applying some logic.
Similarly, if the method of transmission is POST, you can use request.POST as query dictionary for rendering the data from the form into views.
URL trên được thêm vào thuộc tính tên của thẻ đầu vào và tên được nhập vào biểu mẫu. Đây là cách yêu cầu GET hoạt động bất cứ số lượng đầu vào nào họ sẽ được thêm vào URL để gửi dữ liệu đến phần cuối của ứng dụng. Hãy để kiểm tra cách cuối cùng để có được dữ liệu này trong chế độ xem của chúng tôi để logic có thể được áp dụng dựa trên đầu vào. & Nbsp; trong chế độ xem.pypyhome.html
Bây giờ khi chúng ta điền vào biểu mẫu, chúng ta có thể thấy đầu ra trong thiết bị đầu cuối như dưới đây:
retree Dữ liệu từ biểu mẫu thành chế độ xem.
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 02
Trong nhà.html
HTML
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<def register(request): username = request.POST['username'] ... 9 def register(request): username = request.POST.get('username') 3def register(request): username = request.POST['username'] ... 2def register(request): username = request.GET.get('username') 5 def register(request): username = request.GET.get('username') 6def register(request): username = request.POST['username'] ... 2def register(request): username = request.GET.get('username') 8<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 7
<<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 0 <input type="text" name="username" class="form-control" placeholder="Desired Username" /> 1 <input type="text" name="username" class="form-control" placeholder="Desired Username" /> 2<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 3 method
<input type="text" name="username" class="form-control"
placeholder="Desired Username" />
8<def register(request):
username = request.POST['username']
...
0 def register(request):
username = request.POST['username']
...
1def register(request):
username = request.POST['username']
...
2223def register(request):
username = request.POST['username']
...
4____________<input type="text" name="username" class="form-control"
placeholder="Desired Username" />
7
Now, in views.py let’s check what request.POST has got.
Python3
request.POST3 request.GET8request.POST5 method0
method1 method2
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8<form>5<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 46
<input type="text" name="username" class="form-control" placeholder="Desired Username" /> 8method4 method5method6method7
Vì chúng tôi có mọi thứ được thiết lập, hãy để chúng tôi chạy máy chủ chạy Python Manage.py và kiểm tra xem biểu mẫu có trên trang chủ không.
Theo mặc định, mọi biểu mẫu từng được viết bằng HTML đều thực hiện yêu cầu nhận ở phần cuối của ứng dụng, yêu cầu GET thường hoạt động bằng cách sử dụng các truy vấn trong URL. Hãy để cho thấy nó bằng cách sử dụng biểu mẫu trên, điền vào biểu mẫu bằng tên của bạn và hãy để kiểm tra những gì xảy ra.