【发布时间】:2017-12-03 03:48:07
【问题描述】:
我希望一个页面上有多个表单。到目前为止,我有一个简单的表单,用户输入数字并返回一个数字。当我尝试在同一页面上添加另一个表单(在我的模型中将是 Post2)时,我不能。我最终得到了两次相同的表格。
感谢所有帮助。
进行了编辑添加以尝试使第二种形式正常工作 urls.py
from django.conf.urls import url
from django.contrib import admin
from amat.views import AmatView
from . import views
urlpatterns = [
url(r'^$', AmatView.as_view(), name='home'),
]
oddsmat.html
{% 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 }}
views.py
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)
models.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)
forms.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')
【问题讨论】:
-
你遇到了什么错误?
-
我不明白你在问什么。你在哪里使用两种形式?
-
您有 2 个模型,但只有一个模型表单。这里的每个模型都需要一个 ModelForm(内部 Meta 类说明了一切:model = Post)
-
您不将第二个表单传递给您的模板,您应该使用构造函数创建它,然后传递,而不是单独的字段,并且您只创建一个 Post 表单,而您应该创建一个 Post2 表单,如好吧。
-
@Fazil Zaid 我无法让两个表单在一个表单上工作。我没有收到错误我根本不知道从哪里开始(我已经做了几个小时)
标签: python django forms templates