【问题标题】:django form doesn't go inside postdjango 表单不会进入帖子内部
【发布时间】:2020-08-15 23:51:05
【问题描述】:

我只是想能够读取用户在服务器表单上输入的值。 我注意到它永远不会进入视图函数的这一部分

 if request.method == 'POST':

如果我遗漏了什么,请告诉我。我已经为视图、url、表单、html 文件包含了我的代码 sn-ps:

urls.py=>
urlpatterns = [
    path('create',views.create),
    path('create/',views.create),
    path('download', views.download),
]

checklistform.py=>
class ContactForm(forms.Form):
    name = forms.CharField()

create.html=>
<!DOCTYPE html>
<html lang="en>
<head>
</head>
<body>
 <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="OK">
 </form>
</body>
</html>

views.py =>
from django.shortcuts import render, HttpResponseRedirect, HttpResponse, redirect
from .checklistform import ContactForm

def create(request):
    print("letssee")
    print(request.method)
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            print(name)

    form=ContactForm()
    return render(request, 'create.html', {'form': form})

在 pycharm => 中看到的输出(我们可以看到它永远不会进入 if)

[15/Aug/2020 23:47:03] "GET / HTTP/1.1" 404 2038
letssee
GET
[15/Aug/2020 23:47:08] "GET /imschecklist/create HTTP/1.1" 200 364

谢谢, 尼克

【问题讨论】:

  • 提交表格了吗?我在表单中没有看到action=...
  • 我确实提交了表格。我还尝试在 html 文件上使用以下内容,但这没有任何区别:

标签: python django forms django-forms request


【解决方案1】:

&lt;html lang="en&gt; 更改为 &lt;html lang="en"&gt;。你错过了双引号。

【讨论】:

  • 我也试过了,但还是有同样的问题。
  • &lt;html lang="en&gt; 更改为 &lt;html lang="en"&gt;。你删除双引号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多