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

Rendering HTML forms (get and post) in Django

編輯:Python
Django  Usually called “ Frame containing battery ”, Because it has default settings for everything , And it has the function of helping anyone to develop the website quickly . Talking about forms , stay  HTML  in , Form is  <form>...</form>  A collection of elements within , Allows visitors to perform tasks such as entering text 、 Select options 、 Operate on objects or controls , Then send the information back to the server . Basically , It is a collection of data for any purpose , Including saving it in the database or getting data from the database .Django  Support all types  HTML  Forms and render data from them into views , In order to use various logical operations for processing . To learn about  HTML  More information about the form , Please visit How to use  Django Forms  create form ?.Django  It also provides Django Forms Built in features of , It's like Django Models equally . Can be in  Django  Create forms in and use them to get data from users in a convenient way . To start using forms , You need to be familiar with the GET  and  POST  request .
  • GET: by comparison ,GET  Bind the submitted data into a string , And use it to form a  URL.URL  Contains the address where data must be sent , And data keys and values . If you are in  Django  Search in the document , You can see this , This will result in a  https://docs.djangoproject.com/search/?q=forms&release=1  Of  URL.
  • POST: Any request that can be used to change the state of the system ( for example , Requests to make changes in the database ) Should be used  POST.

stay  Django  Medium rendering  HTML  Form description

Use examples to illustrate Django  Forms . Consider a person named  geeksforgeeks  Project , It has a name called  geeks  Applications for .
Let's create a simple  HTML  Form to show how to enter data from the user and use it in the view . stay  geeks > templates > home.html  Enter the following code in
<form action = &quot;&quot; method = &quot;get&quot;>
<label for=&quot;your_name&quot;>Your name: </label>
<input id=&quot;your_name&quot; type=&quot;text&quot; name=&quot;your_name&quot;>
<input type=&quot;submit&quot; value=&quot;OK&quot;>
</form>

Now we're going to render it in our view , We need to modify... For geek apps  urls.py. stay  geeksforgeeks > urls.py  Enter the following code in
from django.urls import path

# importing views from views..py
from .views import geeks_view

urlpatterns = [
path('', home_view ),
]

Now? , Let's switch to  home_view  And start checking how we will get the data . come from  Django  in  HTML  All the data of the form is used as  JSON  Object transfer , It's called a request . Let's first create a view , Then we will try all the ways to get data from the form .
from django.shortcuts import render

#  Create your view here
def home_view(request):

#  The logic of the view will be implemented here
return render(request, &quot;home.html&quot;)

When everything is in place , Let's run  Python manage.py run server  And check if the form exists on the home page .


By default , Each uses  HTML  Written forms are sent to the back end of the application  GET  request ,GET  Requests typically use  URL  To work with queries in . Let's use the table above to demonstrate , Fill out the form in your name , Let's see what happens .


above  URL  With input tag attached Name attribute and the name entered in the form . This is it.  GET  How the request works , Whether they will be attached to  URL  To send data to the back end of the application . Let's examine how we finally get this data in our view , So that the logic can be applied according to the input .&nbsp; stay views.py
from django.shortcuts import render

#  Create your view here
def home_view(request):
print(request.GET)
return render(request, &quot;home.html&quot;)

Now? , When we fill out the form , We can see the following output in the terminal :


request.GET Return a query Dictionary , It can be like any other  python  Access it like a dictionary , Finally, use its data to apply some logic .&nbsp; Again , If the transmission mode is  POST, You can use  request.POST  As a query Dictionary , Render the data in the form to the view . stay home.html in
<form action = &quot;&quot; method = &quot;POST&quot;>
{% csrf_token %}
<label for=&quot;your_name&quot;>Your name: </label>
<input id=&quot;your_name&quot; type=&quot;text&quot; name=&quot;your_name&quot;>
<input type=&quot;submit&quot; value=&quot;OK&quot;>
</form>

Please note that , Whenever we create a form request , For safety's sake ,Django  Will ask you to add  {% csrf_token %}&nbsp; Now? , stay  views.py  in , Let's check  request.POST  what are you having? .
from django.shortcuts import render

#  Create your view here
def home_view(request):
print(request.POST)
return render(request, &quot;home.html&quot;)

Now? , When we submit the form , It will display the following data .


such , People can use this data to query the database or use some logical operations to process , And use the context dictionary to pass it to the template .
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved