【问题标题】:No Output from views.py file after getting POST from template file (Django)从模板文件(Django)获取POST后views.py文件没有输出
【发布时间】:2021-10-07 06:13:50
【问题描述】:

我一直在尝试使用 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>

请帮忙。提前致谢。

【问题讨论】:

  • 您确定您输入的数据已经过验证吗?你能看到终端上的print("Validation SUCCESS") 吗?否则您应该在表单中显示验证错误,以便在验证失败时获得反馈。
  • 不,验证行也没有出现。我现在添加了一个 else 条件来检查它。 else: print("Not Validated") 这也没有显示任何输出。谢谢

标签: python django forms post


【解决方案1】:

更正:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2022-10-24
      • 2018-02-27
      • 2014-12-03
      • 1970-01-01
      • 2019-04-07
      • 2021-08-12
      • 2012-06-14
      • 1970-01-01
      相关资源
      最近更新 更多