django获取表单数据

In this tutorial, we’ll see how we can fetch text/data from HTML forms in our templates to views.py to process it and then send that data to another html page.

在本教程中,我们将看到如何从模板中HTML表单中获取文本/数据到views.py进行处理,然后将数据发送到另一个html页面。

Before starting, let’s have a look on our project folder that what files we have and in what directory these files are stored.

在开始之前,让我们在项目文件夹中查看我们拥有的文件以及这些文件存储在哪个目录中。

django获取表单数据_如何在Django中将数据从模板表单获取到视图

Here we’ve home.html in our templates and views.py in my_website directory in our project folder. Here home.html is our homepage and the path of templates has been already set in our settings.py.

在这里, 模板中home.html ,在项目文件夹中的my_website目录中有views.py 。 这里home.html是我们的主页,并且模板的路径已经在settings.py中设置

Now you’ve the basic idea of our project files and directories so let’s see how to fetch data in our views.py from home.html and send that data to another HTML page named as newpage.html after processing it.

现在,您已经有了我们项目文件和目录的基本概念,下面让我们看看如何从home.html获取views.py中的数据,并在处理后将数据发送到另一个名为newpage.html的 HTML页面。

home.html  

home.html  

1
2
3
4
5
6
7
<h1>HOMEPAGE</h1>
<br>
<form action="{% url 'my_function' %}">
<textarea name="fulltextarea" cols=40 rows=10></textarea>
<br />
<input type="submit" value="fetch data!"/>
</form>
1
2
3
4
5
6
7
< h1 > HOMEPAGE < / h1 >
< br >
< form action = "{% url 'my_function' %}" >
< textarea name = "fulltextarea" cols = 40 rows = 10 > < / textarea >
< br / >
< input type = "submit" value = "fetch data!" / >
< / form >
django获取表单数据_如何在Django中将数据从模板表单获取到视图

Here we have a form having a text area and a submit button. When user will press that button named as fetch data then his request will be redirected to the urls.py and look for the name my_function as mentioned in action attribute of form in above HTML page. 

在这里,我们有一个具有文本区域和提交按钮的表单。 当用户按下名为获取数据的按钮时,他的请求将被重定向到urls.py并查找名称my_function,如上面HTML页面中form的 action属性中所述  

urls.py

urls.py

1
2
3
4
5
6
7
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('newpage/',  views.new_page,  name="my_function")
]
1
2
3
4
5
6
7
from django . urls import path
from . import views
urlpatterns = [
path ( '' , views . home ) ,
path ( 'newpage/' ,    views . new_page ,    name = "my_function" )
]

Here we’ve a path for the name my_function. Here we have a new page url newpage/ and a function views.new_page, so the request will be redirected to the function views.new_page

在这里,我们有一个名为my_function的路径 这里我们有一个新页面的URL newpage /和一个函数views.new_page,所以请求将被重定向到函数views.new_page

views.py

views.py

1
2
3
4
5
6
7
8
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def new_page(request):
data = request.GET[‘fulltextarea’]
return render(request, 'newpage.html', {'data':data})
1
2
3
4
5
6
7
8
from django . shortcuts import render
def home ( request ) :
return render ( request , 'home.html' )
def new_page ( request ) :
data = request . GET [ ‘ fulltextarea ’ ]
return render ( request , 'newpage.html' , { 'data' : data } )

Now here we’ve a function new_page which receives user request and in next line we’re retrieving the text entered in our home.html form. Here we’re using the request object’s GET method to get the text from our textarea in our html form. All we have to do is just pass the name of our textarea inside the square brackets of GET method. Now we’ve all the text entered by the user inside our data variable. So now we can process or do any thing with our string variable data (like counting words, counting frequency of each word, replacing specific words, fetching entered email or phone numbers and all the operations that we can perform with strings in Python). But here we’re simply returning our information to a new html page named as newpage.html.

现在,这里有一个函数new_page ,它接收用户请求,并在下一行中检索在home.html表单中输入的文本 在这里,我们使用请求对象的GET方法从html表单的文本区域中获取文本 我们所要做的只是在GET方法的方括号内传递文本区域的名称。 现在,我们将用户输入的所有文本都放入了数据变量中。 因此,现在我们可以使用字符串变量数据来处理或做任何事情(例如,对单词进行计数,对每个单词的计数频率,替换特定单词,获取输入的电子邮件或电话号码以及我们可以在Python中使用字符串执行的所有操作)。 但是在这里,我们只是将信息返回到名为newpage.html的新html页面

newpage.html

newpage.html

1
2
3
<h1>New page</h1>
{{ data }}
1
2
3
< h1 > New page < / h1 >
{ { data } }

Here we’ve a simple heading New page and printing the value of the key data that is sent from our views.py’s new_page function.

在这里,我们有一个简单的标题“ 新页面”,并打印从views.py的new_page函数发送的关键数据的值

Here’s the complete output of this project.

这是该项目的完整输出。

Pressing fetch data button.

按提取数据按钮。

django获取表单数据_如何在Django中将数据从模板表单获取到视图

That’s all.

就这样。

I hope, now you’ve the basic idea of retrieving information from the HTML form to views to process it and also how to send the information from views to HTML document.

我希望,现在您有了从HTML表单中检索信息以对其进行处理的基本思想,以及如何将视图中的信息发送到HTML文档的基本思想。

If you’ve any query related to this tutorial then please let us know in comment box.

如果您对本教程有任何疑问,请在评论框中告诉我们。

翻译自: https://www.thecrazyprogrammer.com/2018/11/how-to-fetch-data-from-template-forms-to-views-in-django.html

django获取表单数据

相关文章:

  • 2021-10-03
  • 2021-12-31
  • 2021-10-03
  • 2021-10-03
  • 2021-12-05
  • 2021-07-10
  • 2021-12-14
  • 2018-06-08
猜你喜欢
  • 2022-01-01
  • 2021-12-21
  • 2022-12-23
  • 2021-10-03
  • 2022-01-01
  • 2021-09-28
相关资源
相似解决方案