我想在一个页面上有多个表单。到目前为止,我有一个简单的表单,用户输入数字并返回一个数字。当我试图在同一页面上添加另一个表单(我的模型中将是Post2)时,我无法。我最终得到了两次相同的表格。
所有帮助表示赞赏。
进行编辑添加以尝试使第二个表单正常工作 urls.py
{% load staticfiles %}
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" name="form1"/>
</form>
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" name="form2"/>
</form>
...
{{ round1 }}
{{ round2 }}
{{ round3 }}
{{ betround1 }}
{{ betround2 }}
{{ betround3 }}
<br>
its the same as {{ thesameas }}
oddsmat.html
from django.shortcuts import render
from django.views.generic import TemplateView
from amat.forms import AmatForm
# Create your views here.
class AmatView(TemplateView):
template_name = 'oddsmat.html'
def get(self, request):
form = AmatForm()
args = {
"form": form
}
return render(request, self.template_name, args)
def post(self, request):
form = AmatForm(request.POST)
if form.is_valid():
round1 = form.cleaned_data['round1']
round2 = form.cleaned_data['round2']
round3 = form.cleaned_data['round3']
form = AmatForm()
betround1 = ((round2+1)*(round3+1))/((round1)*(round2+2)+(round2)*(round3+2)+(round3)*(round1+2)+3)
betround2 = ((round1+1)*(round3+1))/((round1)*(round2+2)+(round2)*(round3+2)+(round3)*(round1+2)+3)
betround3 = ((round2+1)*(round1+1))/((round1)*(round2+2)+(round2)*(round3+2)+(round3)*(round1+2)+3)
thesameas = round1*betround1-betround2-betround3
args = {
"form": form,
"round1":round1,
"round2":round2,
"round3":round3,
"betround1":betround1,
"betround2":betround2,
"betround3":betround3,
"thesameas":thesameas,
}
return render(request, self.template_name, args)
class AmatView2(TemplateView):
template_name = 'oddsmat.html'
def get(self, request):
form = AmatForm2()
args = {
"form": form
}
return render(request, self.template_name, args)
def post(self, request):
form = AmatForm2(request.POST)
if form.is_valid():
round10 = form.cleaned_data['round10']
round11= form.cleaned_data['round11']
form = AmatForm2()
args = {
"form": form,
"round10":round10,
"round11":round11,
}
return render(request, self.template_name, args)
views.py
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Post(models.Model):
round1 = models.DecimalField(max_digits=12, decimal_places=5)
round2 = models.DecimalField(max_digits=12, decimal_places=5)
round3 = models.DecimalField(max_digits=12, decimal_places=5)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Post2(models.Model):
round10 = models.DecimalField(max_digits=12, decimal_places=5)
round11 = models.DecimalField(max_digits=12, decimal_places=5)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
models.py
from django import forms
from amat.models import Post,
class AmatForm(forms.ModelForm):
class Meta:
model = Post
fields = ('round1', 'round2', 'round3')
class AmatForm2(forms.ModelForm):
class Meta:
model = Post2
fields = ('round10', 'round11')
forms.py
pyspark.ml.linalg.SparseMatrix
答案 0 :(得分:0)
您有两个单独的视图,它们只返回特定的表单。在你的模板中,你重复代码,因此你得到它两次。返回支持任意数量的表单。在您的视图中,您可以这样做:
form1 = AmatForm()
form2 = AmatForm2()
args = {
'form2' = form1
'form2' = form2
}
return render(request, self.template_name, args)
然后,您可以在模板中使用:
{{ form1.as_p }}
{{ form2.as_p }}
我建议将表单字段设为可选,然后检查是否输入了任何数据,否则会遇到空表单字段的问题。