为什么按钮提交是"positional argument"



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

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 44444 3.0

数字_2 55555 3.0

目标

请求

#当我输入两个数值(浮点),Numeric_1=4444443.0和Numeric_2=5555553.0时,我得到的Django ERROR是=>"addition()接受2个位置参数,但给定了3个"。

Submit操作被认为是我的简单"加法"函数的"位置参数"。

我该如何避免这种情况?从我的-views.py中可以看出,我调用了函数"addition",并传递了-numberic_1=444444 3.0和numeric_2=555555 3.0,参数,我不想将FORM SUBMIT ACTION传递给"addition."函数。

我的观点.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') # 

记录我非常简单的失误-"加法"函数需要DEFAULT位置参数"self",因此通过在"加法"功能中添加"self。。并不是Django表单的问题。

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

相关内容

  • 没有找到相关文章

最新更新