从模板文件 (Django) 获取 POST 后,views.py 文件没有输出

时间:2021-08-01 16:22:46

标签: python django forms post

我一直在尝试使用 django 将详细信息填写在表单中,返回到 python 文件中。一切似乎都很好,但表单详细信息没有显示在终端中。

views.py 文件:

from django.shortcuts import render
from basicapp import forms
# Create your views here. 
def index(request):
return render(request,'basicapp/index.html')

def form_name_view(request):
    form = forms.FormName()
    if request == "POST":
        form = forms.FormName(request.POST)

        if form.is_valid():
            #DO SOMETHING
            print("Validation SUCCESS")
            print("NAME: " + form.cleaned_data['name'])
            print("EMAIL: " + form.cleaned_data['email'])
            print("AVANODA KARUTHTHU: " + form.cleaned_data['text'])


    return render(request,'basicapp/form_page.html', {'form' : form } )

form_page.html 文件:(我已经删除了 Bootstrap CDN)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forms</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, shrink-to-fit=no">                                                                                  
    
</head>
<body>
    <div class="container">
        <div class = "jumbotron">
            <h2> Fill out the form </h2>
            <form method="POST" action = "">
                {{ form.as_p }}
                {% csrf_token %}
                <input type="submit" class = "btn btn-primary" value = "Submit">
            </form>
        </div>
    </div>
</body>
</html>

请帮忙。提前致谢。

2 个答案:

答案 0 :(得分:1)

更正:

if request.method == "POST":index 函数下

答案 1 :(得分:0)

更新:找到解决方案。请参考附图。