猿问

django 表单不在帖子内

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


 if request.method == 'POST':

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


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


慕斯709654
浏览 157回答 1
1回答

函数式编程

<html lang="en>改为<html>.&nbsp;你错过了双引号。
随时随地看视频慕课网APP

相关分类

Python
我要回答