如何让Django表单数据并将其用于进一步处理和呈现?

时间:2017-07-25 05:57:55

标签: django django-forms

我是Django的新手并且用我自己学习我能够构建一些基本的应用程序,我可以说我的关于Django视图,网址和模型的概念非常清楚,但形式对我来说是相当混乱的主题。我无法处理它。让我们假设一种情况。

我创建了一个表单“Calculator”从用户那里获得了两个带有数学运算符加法,减法或乘法的值:

示例:

我的表格:

value_1 = 8
Value_2 = 6
Math_op = +

现在我在视图中定义了一个计算器函数,它从表单中获取值并进行一些数学运算并生成这样的结果

Answers = 14

现在我想将此结果或应答变量值转移到新网页以显示计算器事件的放置。

我的困惑是如何为这种情况定义一个url,以及如何定义一个从模板获取值并将此值呈现给另一个表单模板的方法。

我使用过的代码,请给出:更新代码

Forms.py

from django import forms

class NameForm(forms.Form):


    first_value = forms.CharField(label='First Value', max_length=100)
    Second_value = forms.CharField(label='Second Value', max_length=100)
    operator     = forms.CharField(label='Operator', max_length=100)

Views.py

rom django.http import HttpResponseRedirect
from django import forms
from .forms import NameForm

def get_name(request):

    if request.method == 'POST':

        form = NameForm(request.POST)

        if form.is_valid():

            value_1 = form.cleaned_data['first_value']
            value_2 = form.cleaned_data['Second_value']
            ope = form.cleaned_data['operator']

            if ope == "+":
                ans = int(value_1) + int(value_2)
            elif ope == "-":
                ans = int(value_1) - int(value_2)
            elif ope == "*":
                ans = int(value_1) * int(value_2)

            else:
                print "Values are not correct"

            return HttpResponseRedirect('/ans_page/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = NameForm()

    return render(request, 'PEP/name.html', {'form': form})

def ans_page(request):
   ans = request.session['ans']
   return render(request,'PEP/ans.html',{'ans':ans})

的url:

    from django.conf.urls import url
    from django.contrib import admin
    from . import views

    urlpatterns = [

    url(r'^$', views.get_name, name='get_name'),
    url(r'^/ans_page$', views.ans_page, name='ans_page'),
    ]

模板:

<form action="/ans_page/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

模板2:ans.html

{% if  ans %}
{{ ans }}
{% endif %}

错误:

KeyError at /ans_page/
'ans'
Request Method: POST
Request URL:    http://127.0.0.1:8000/ans_page/
Django Version: 1.10
Exception Type: KeyError
Exception Value:    
'ans'
Exception Location: /home/jai/Desktop/Django_form/local/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py in __getitem__, line 57
Python Executable:  /home/jai/Desktop/Django_form/bin/python
Python Version: 2.7.12
Python Path:    
['/home/jai/Desktop/Django_form/src',
 '/home/jai/Desktop/Django_form/lib/python2.7',
 '/home/jai/Desktop/Django_form/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/jai/Desktop/Django_form/lib/python2.7/lib-tk',
 '/home/jai/Desktop/Django_form/lib/python2.7/lib-old',
 '/home/jai/Desktop/Django_form/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/jai/Desktop/Django_form/local/lib/python2.7/site-packages',
 '/home/jai/Desktop/Django_form/lib/python2.7/site-packages']
Server time:    Tue, 25 Jul 2017 08:44:15 +0000

1 个答案:

答案 0 :(得分:1)

除了你需要另一个视图功能和一个显示结果的网址外,你大部分都完成了所有部分,

所以在urls.py

urlpatterns = [
    url(r'^$', views.get_name, name='get_name'),
    url(r'^/ans_page$', views.ans_page, name='ans_page'), // add this
    ]

创建视图

def ans_page(request):
    ans = request.session['ans']
    return render(request,'PEP/ans.html',{'ans':ans})

并在ans.html

{% if  ans %}
{{ ans }}
{% endif %}

并在get_name视图中

request.session['ans'] = ans
return HttpResponseRedirect('/ans_page/')

并将表单更改为此

<form action="" method="post">