【问题标题】:Django processing form data without having to create form with DjangoDjango 处理表单数据而无需使用 Django 创建表单
【发布时间】:2020-10-01 01:24:29
【问题描述】:

所以我编写了一个 HTML 页面,所有输入都准备就绪。我打算在我的 python HTTP 服务器上使用它,但我将使用 Django,因为人们一直告诉我它更适合服务器处理。我在 django 上玩过 forms.py,但问题是你必须在 forms.py 中创建表单,然后它为你构造 HTML。我已经准备好 HTML,所以有没有办法让我只处理表单数据,而不必在 forms.py 中再次重写我的整个表单?

这是我希望 Django 处理和打印的 HTML 和表单:

{% load static %}<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="{% static 'polls/style.css' %}">
  </head>
<body>
    <h1 class="mx-auto text-center mt-8 px-0 py-8 border border-4 border-solid border-gray-600"
        style="width: 700!important;">CHECKBOX INPUT</h1>
    <div class="flex h-full mx-auto">
      <form action="/polls/thanks/" method="post">
           {% csrf_token %}
           <div class="w-3/4 py-10 px-8">
                <table class="table-auto">

                    <thead>
                        <tr>
                            <th class="py-10 h-4">
                                <div class="mr-64">
                                    <input type="checkbox" class="form-checkbox h-8 w-8">
                                    <label class="ml-4">test</label>
                                </div>
                            </th>
                        </tr>
                        <tr>
                            <th class="py-10 h-4">
                                <div class="mr-64">
                                    <input type="checkbox" class="form-checkbox h-8 w-8">
                                    <label class="ml-4">test</label>
                                </div>
                            </th>
                        </tr>
                        <tr>
                            <th class="py-10 h-4">
                                <div class="mr-64">
                                    <input type="checkbox" class="form-checkbox h-8 w-8">
                                    <label class="ml-4">test</label>
                                </div>
                            </th>
                        </tr>
                        <tr>
                            <th class="py-10 h-4">
                                <div class="mr-64">
                                    <input type="checkbox" class="form-checkbox h-8 w-8">
                                    <label class="ml-4">test</label>
                                </div>
                            </th>
                        </tr>
                        <tr>
                            <th class="px-4  py-10 h-4">
                                <div class="mx-auto">
                                    <span>TEXT INPUT:</span>
                                    <input type="text" class="form-input mt-4">
                                    <select>
                                        <option value="value1" selected>DROPDOWN</option>
                                        <option value="valeu2">Value 2</option>
                                        <option value="valeu3">Value 3</option>
                                    </select>
                                </div>

                            </th>
                        </tr>
                        <tr>
                            <th class="px-4  py-10 h-4">
                                <div class="mx-auto">

                                </div>

                            </th>
                        </tr>
                        <tr>
                            <th class="px-4  py-10 h-4">
                                <div class="mx-auto">
                                    <input type="submit" value="Submit" class="bg-gray-600 p-4 border-0 border-solid rounded-lg">
                                </div>

                            </th>
                        </tr>
                    </thead>

                </table>
            </div>
        </form>
    </div>
</body>
</html>

谢谢

【问题讨论】:

  • 这取决于你的html。
  • 如果有帮助,我会把它添加到帖子中

标签: python django


【解决方案1】:

当然,您不必创建模型表单,它们只是为了方便而制作的。只需提交您制作的表单,然后在相应的视图中,您就可以使用标签名称从请求中获取数据。当然,如果没有名称,您应该在标签中添加名称。例如:&lt;select name='tag_name'&gt;

以这种方式获取您的数据:

data = request.POST.get('tag_name')

【讨论】:

  • 所以我尝试了这个并编辑表单以包含 csrf_token:&lt;form action="/polls/thanks/" method="post"&gt; {% csrf_token %} 但我仍然收到 403 CSRF 验证失败。有什么想法吗?
  • 在这里我提出了一个单独的问题,如果你能提供帮助会很棒:) stackoverflow.com/questions/64160784/…
猜你喜欢
  • 2016-06-14
  • 2015-07-04
  • 2021-10-05
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 2012-08-09
  • 2018-01-07
  • 1970-01-01
相关资源
最近更新 更多