Django 表单

上手表单处理实例

有时候我们需要在前端用 get 或 post 方法提交一些表单数据。在后端处理数据时,我们需要接受这些数据。本章就来介绍 Django 如何对表单数据进行接收操作的。

为了完成实验,我们先来写一个网页,其中用到 html 表单的知识。首先我们来写一个计算 a 和 b 之和的简单应用。在 learn 的 templates 目录之下,保存一个名为 index.html 的模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<body>
<p>Please input 2 numbers:</p>

<form action="/add/" method="get">
a: <input type="text" name="a"> <br>
b: <input type="text" name="b"> <br>

<input type="submit" value="submit">
</form>

</body>
</html>

网页的值传到服务器是通过 或者