0

在一个类中有一个简单的函数。功能如下:-

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

希望用户在 Django 表单中输入两个数值,然后将它们相加并显示结果。在 HTML 页面中有如下代码:-

<form class="form" action="" method="post" name="NameForm" enctype=multipart/form-data>
          <div class="control-group">

          <div class="controls">
            <label for="user_input_1">Enter Numeric Input - 1: </label>
          {% csrf_token %}
           <input id="user_input_1" name="user_input_1"  value = "{{NameForm.user_input_1}}" type="text" placeholder="Enter Numeric Input - 1:" class="form-control input-xlarge search-query center-display"  required="True">
           </div>

 <div class="controls">
 <label for="user_input_1">Enter Numeric Input - 2: </label>
 <input id="user_input_2" name="user_input_2"  value = "{{NameForm.user_input_1}}" type="text" placeholder="Enter Numeric Input - 2:" class="form-control input-xlarge search-query center-display"  required="True">
 </div><!-- controls -->
 </div><!-- control-group -->

 <div class="control-group form-actions">
 <div class="controls">
 <button class="btn btn-primary btn-sm" value="Gogogo!" >Submit</button>
 </div><!-- controls -->
 </div><!-- control-group -->
 </form>

在我的 -views.py

sum_1 = obj.addition(numeric_1,numeric_2)# Simple Addition Function 

 ...

▼ 局部 vars 变量值形式

数字_1 444443.0

数字_2 555553.0

对象

要求

# 当我输入两个数值(浮点数),numeric_1 = 444443.0 和 numeric_2 = 555553.0 时,我得到的 Django 错误是 =>“addition() 需要 2 个位置参数,但给出了 3 个”。

提交操作被视为我简单的“加法”函数的“位置参数”。

我怎样才能避免这种情况 - 从 my-views.py 中可以看出,我正在调用函数“addition”并将 - numeric_1 = 444443.0 和 numeric_2 = 555553.0 参数传递给它,我不想将 FORM SUBMIT ACTION 传递给“加”功能。

我的views.py 如下所示:-

def calc_a(request):

obj = calc_1() ## 
if request.method == 'POST':
# create a form instance and populate it with data from the request:

    form = NameForm(request.POST) ### NameForm is a Form in app_1/forms.py 
    #print("DHANK______Form Submitted with NUMBER")## ok...
    #result_1 = str(form["user_input_1"].value()) ## keep this INPUT as STR - Not FLOAT 
    #result_2 = str(form["user_input_2"].value()) ## keep this INPUT as STR - Not FLOAT 
    print("   "*30)
    #print("_NUMBER 1__SUBMITTED ___________",result_1) # terminal prints for stepping through code
    print("   "*30)
    #print("_NUMBER 2__SUBMITTED ___________",result_2)
    print("   "*30)
    #print("DHANK______Form Submitted with NUMBER_",form["user_input_1"].value()) ## ok...
    # check whether it's valid:
    if form.is_valid():
        #print("DHANK______Form Submitted is Valid_______")  ### DHANK -- prints - if Form is Valid 
        numeric_1 = float(form["user_input_1"].value())
        numeric_2 = float(form["user_input_2"].value())

        print("DHANK___numeric_2_______",numeric_2)
        print("DHANK___numeric_1________",numeric_1)

        sum_1 = obj.addition(numeric_1,numeric_2)  ### call the "addition" Function 
        print("sum_1____===",sum_1)

return render(request, 'app_1/calc_a.html') # 
4

1 回答 1

0

记录我非常简单的滑点-“addition”函数需要默认位置-参数“self”,因此通过在“addition”函数中添加“self”来解决如下所示的问题..并不是真正的Django表单问题。

def addition(self,a,b):
    return a+b
于 2017-11-18T08:47:18.750 回答