Python 未使用django表单将数据保存到数据库
我正在开发一个django应用程序,它包含一个表单。我定义了模型并进行了迁移。但是数据没有保存到数据库中。当我使用提交表单时,应用程序的URL会被弄乱 这是到目前为止我的代码 models.py forms.py views.py index.html 这些是我检查过的一些链接,但是答案不起作用 如果您没有发出POST请求,则应在Python 未使用django表单将数据保存到数据库,python,html,django,django-models,django-forms,Python,Html,Django,Django Models,Django Forms,我正在开发一个django应用程序,它包含一个表单。我定义了模型并进行了迁移。但是数据没有保存到数据库中。当我使用提交表单时,应用程序的URL会被弄乱 这是到目前为止我的代码 models.py forms.py views.py index.html 这些是我检查过的一些链接,但是答案不起作用 如果您没有发出POST请求,则应在标记中指定method=“POST”: <form method="post"> {% csrf_token %} {
标记中指定method=“POST”
:
<form method="post">
{% csrf_token %}
{{ testForm.name|as_crispy_field }}
{{ testForm.email|as_crispy_field }}
{{ testForm.phone|as_crispy_field }}
<input type="submit" value="check" class="save btn submit_button">
</form>
{%csrf_令牌%}
{{testForm.name}as_crispy_field}
{{testForm.email}as_crispy_field}
{{testForm.phone | as_crispy_field}
默认情况下,方法是GET。您实际上可以看到这一点,因为数据是在URL的查询字符串中传递的。因此,这意味着request.method==“POST”
检查将失败,因此,它确实不会将数据保存到数据库。如果没有发出POST请求,则应在
标记中指定method=“POST”
:
<form method="post">
{% csrf_token %}
{{ testForm.name|as_crispy_field }}
{{ testForm.email|as_crispy_field }}
{{ testForm.phone|as_crispy_field }}
<input type="submit" value="check" class="save btn submit_button">
</form>
{%csrf_令牌%}
{{testForm.name}as_crispy_field}
{{testForm.email}as_crispy_field}
{{testForm.phone | as_crispy_field}
默认情况下,方法是GET。您实际上可以看到这一点,因为数据是在URL的查询字符串中传递的。因此,这意味着
request.method==“POST”
检查将失败,因此,它确实不会将数据保存到数据库。您需要在表单标记method=“POST”中指定方法,并且需要在单击提交或检查按钮后在表单标记中给出您想要去的路径或url
<form method="post" acton="Enter the path or url here">
{% csrf_token %}
{{ testForm.name|as_crispy_field }}
{{ testForm.email|as_crispy_field }}
{{ testForm.phone|as_crispy_field }}
<input type="submit" value="check" class="save btn submit_button">
</form>
{%csrf_令牌%}
{{testForm.name}as_crispy_field}
{{testForm.email}as_crispy_field}
{{testForm.phone | as_crispy_field}
还可以尝试在表单参数中传递请求,如testform(request,data=request.POST),它现在应该可以工作了。您需要在表单标记method=“POST”中指定方法,并且需要在单击提交或检查按钮后在表单标记中给出您想要去的路径或url
<form method="post" acton="Enter the path or url here">
{% csrf_token %}
{{ testForm.name|as_crispy_field }}
{{ testForm.email|as_crispy_field }}
{{ testForm.phone|as_crispy_field }}
<input type="submit" value="check" class="save btn submit_button">
</form>
{%csrf_令牌%}
{{testForm.name}as_crispy_field}
{{testForm.email}as_crispy_field}
{{testForm.phone | as_crispy_field}
还可以尝试在表单参数中传递请求,如testform(request,data=request.POST),它现在应该可以工作。如果不传递
操作
参数,它将使用与当前使用的页面相同的路径,因此严格来说,它不是必需的。如果不传递操作
参数,它将使用与您当前使用的页面相同的路径,因此严格来说,它不是必需的。
http://127.0.0.1:8000/?csrfmiddlewaretoken=BG2i7fSbwG1d1cOlLWcEzy5ZQgsNYzMrhDJRarXkR3JyhetpWvqNV48ExY7xM9EW&name=randomPerson&email=test%40test.com&phone=12345678
<form method="post">
{% csrf_token %}
{{ testForm.name|as_crispy_field }}
{{ testForm.email|as_crispy_field }}
{{ testForm.phone|as_crispy_field }}
<input type="submit" value="check" class="save btn submit_button">
</form>
<form method="post" acton="Enter the path or url here">
{% csrf_token %}
{{ testForm.name|as_crispy_field }}
{{ testForm.email|as_crispy_field }}
{{ testForm.phone|as_crispy_field }}
<input type="submit" value="check" class="save btn submit_button">
</form>