如何区分来自 django 中不同按钮的发布请求?例如,如果我想创建一个计算器,我该如何做才能使“乘”按钮的 POST 与“加”和“减”按钮的 POST 不同?编辑:
这是我的views.py:
from django.shortcuts import render,get_object_or_404
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.views.generic import TemplateView
from django import forms
# Create your views here.
# def startPage(request):
# return render(request, 'multiplication/detail.html')
template_name1 = 'multiplication/detail.html'
template_name2 = 'multiplication/multiplied.html'
class myForm(forms.Form):
quantity1 = forms.IntegerField(required=False)
quantity2 = forms.IntegerField(required=False)
def multiply_two_integers(x,y):
return x*y
def my_view(request):
if request.method == 'POST':
form = myForm(request.POST)
if (form.is_valid()):
x = form.cleaned_data['quantity1']
y = form.cleaned_data['quantity2']
product = multiply_two_integers(x, y)
multiplied = True
return render(request, template_name1, {'form':
form,'product': product, 'multiplied': multiplied })
else:
multiplied = False
form = myForm()
return render(request,template_name1,{'form': form,
'multiplied': multiplied} )
模板名称1:
<h1>Multiplication Function</h1>
<form action = "{% url 'multiplication:my_view' %}"
method = "POST">
{{ form.as_p }}
{% csrf_token %}
<input type = "submit" value ="Multiply">
<input type = "submit" value ="Add">
<!--<button type="submit"> Multiply </button>-->
{% if multiplied %}
<h1 style="color:blue; margin-left:75px;"> Answer:
{{product}}</h1>
{% endif %}
</form>