【问题标题】:How to access data from HTML form in views.py?如何从views.py中的HTML表单访问数据?
【发布时间】:2017-02-21 05:23:52
【问题描述】:

当我完全用 HTML 创建表单时,如何访问 views.py 中的表单数据? 我想在不使用 django 表单类的情况下完全用 HTML 创建一个表单。完成后,如何访问views.py 中的表单字段数据?我想在后端使用数据。请帮帮我

<html>
<head>
 <title>
 HTML forms
 </title>
</head>
<body>

<p> This is a simple form</p>
<form action="" method="post">
{% csrf_token %}
 <fieldset>
 <legend> Personal Information</legend>
 <label for="name">contact_name:<br></label>
 <input type="text" name="contat_name" id="name"><br> 
 <label for="email">contact_email:<br></label>
 <input type="email" name="email" id="email" required placeholder="example@example.com"><br>
 <label for="content">content:<br></label>
 <textarea name="content" rows="10" cols="30">
 </textarea>
 <br>
 <input type="submit" value="submit">
 </fieldset>
 </body>
 </html>

这是我想要访问views.py 中的contact_name 和contact_email 的HTML 表单。如何访问数据?

def htmlform(request):
if request.method=="POST":
    print request.POST.get("contact_name")      
return render(request,"htmlform.html")

当我使用 request.POST.get("contact_name") 它什么都不返回。

【问题讨论】:

  • 显示一个最小的可重现示例
  • 请查看已编辑的字段
  • 对不起大家。这是因为拼写错误。我在 HTML 文件中输入了 contat_name 而不是 contact_name

标签: python-2.7 django-forms forms


【解决方案1】:

在 Html 中,您将名称命名为“contat_form”,而在 views.py 中,您尝试将名称访问为“contact_form”

【讨论】:

    【解决方案2】:

    这是因为在您的views.py 中您尝试访问contat_form,而html 表单中的实际字段名称是contact_form。否则你的方法是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多