【问题标题】:Passing data from a django view to a template将数据从 django 视图传递到模板
【发布时间】:2019-02-16 18:49:55
【问题描述】:

我想在计算后将视图中的数据显示到模板中。 View 从 Django 表单中收集数据,然后执行简单的计算。但是,该函数不会重定向到模板。

 views.py

    from . import forms
    from django.http import HttpResponseRedirect
    from django.shortcuts import render,redirect

    def index(request):
        form = forms.InputForm()
        return render(request, 'index.html', {'form': form})


    def addition(a,b):
        c = a + b
        return c


    def input_form_Addition(request):
        if request.method == 'post':
           form = forms.InputForm(request.POST)
           if form.is_valid():
              input1 = form.cleaned_data['input1']
              input2 = form.cleaned_data['input2']
              total = addition(input1,input2)

              return render(request, 'output.html', {'total': total})
        else:
             form = forms.InputForm()

模板(output.html)如下所示:

    <!DOCTYPE html>
    <html lang="en">
    <head>
       <meta charset="UTF-8">
       <title>Forms</title>

    </head>
    <body>

      <p>The Sum is {{ total }}</p>

    </body>
    </html>

我的url路由如下:

    from django.urls import path
    from .views import index
    from simple_addition import views

      urlpatterns = [
           path('', views.index, name='index'),
           path('output/', views.input_form_Addition, name='output')
             ]

我的 Django 表单显示良好,如下所示。

forms.py 从 django 导入表单

    class InputForm(forms.Form):
       input1 = forms.FloatField()
       input2 = forms.FloatField()

并显示在:

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Forms</title>

   </head>
   <body>
      <h2>Addition Page</h2>
      <p>Fill in two numbers to get their sum</p>

      <form method="post">
           {% csrf_token %}
           {{ form.as_p }}
      <input type="submit" value="Add" >
      </form>
    </body>
    </html>

【问题讨论】:

  • redirect 不接受模板。这就是重定向的工作原理。简单来说,重定向意味着您将用户重定向到另一个 url。相反,只需使用 render 函数。
  • render 函数也不显示 output.hml 。而是再次重新加载 输入表单
  • 请修复视图中的缩进。
  • 我已经修复了标识并替换了渲染功能

标签: django django-templates django-views


【解决方案1】:

你能把你的表格代码吗? (模板和forms.py)

编辑:

好的,我找到了你的问题:

<form method="post">
   {% csrf_token %}
   {{ form.as_p }}
<input type="submit" value="Add" >

您错过了帖子表单的操作:

表单模板:

<form method="post" action="/output/">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Add" >
</form>

urls.py

path('output/', views.input_form_Addition, name='output')

views.py

from . import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render,redirect

def index(request):
    form = forms.InputForm()
    return render(request, 'index.html', {'form': form})


def addition(a,b):
    c = a + b
    return c


def input_form_Addition(request):
    form = forms.InputForm(request.POST)

    if form.is_valid():
        input1 = form.cleaned_data['input1']
        input2 = form.cleaned_data['input2']
        total = addition(input1, input2)

        return render(request, 'output.html', {'total': total})

【讨论】:

  • 这应该是评论,而不是答案。
  • 无法以我的声誉添加评论,我是新人
  • 我添加了表单动作。 output.html 会显示出来,但是它不会给出视图中的总变量。
  • 您可以在您的视图上发送结果返回吗?
  • 抱歉……我没听懂。
【解决方案2】:

只需替换函数中的行input_form_Addition(request):

if request.method == 'post':

if request.method == 'POST':

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2015-07-07
    • 2018-02-10
    • 2020-06-08
    • 2017-04-26
    • 2016-09-28
    相关资源
    最近更新 更多