Python 如果不选中ModelForm BooleanField,则不会返回任何POST数据,如果它为';已选中,如果未选中,则另加一个';T
我有一个模型:Python 如果不选中ModelForm BooleanField,则不会返回任何POST数据,如果它为';已选中,如果未选中,则另加一个';T,python,django,Python,Django,我有一个模型: class Customer(models.Model): name = models.CharField(max_length=200) email = models.EmailField() company = models.CharField(max_length=100, blank=True) breakfast = models.BooleanField(blank=True) dinner = models.BooleanFi
class Customer(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
company = models.CharField(max_length=100, blank=True)
breakfast = models.BooleanField(blank=True)
dinner = models.BooleanField(blank=True)
training = models.BooleanField(blank=True)
joined = models.DateTimeField(auto_now_add=True)
还有一个观点:
def cust_form(request):
if request.method == 'POST':
form = CustForm(request.POST)
form_type = 'Customer'
if form.is_valid():
email = request.POST['email']
name = request.POST['name']
if request.POST['training'] == True:
mail.mailsend(name, email, cus_type='train')
else:
mail.mailsend(name, email, cus_type='stand')
form.save()
return Redirect('/cr/')
return render_to_response('cust_form.html',
{'form': form,
'form_type': form_type},
context_instance=RequestContext(request))
如果他们选择了培训,我想发一封电子邮件,如果他们没有,我想发一封电子邮件。但是,如果未选中收音机框,则在/Customer/
唯一的解决方案是带有yes/no的ForeignKey吗?将行更改为
if request.POST.get('training', False):
换行
if request.POST.get('training', False):
您只需要检查值是否存在
if 'training' in request.POST:
或
(无论如何,您不应该与True进行比较。)您只需要检查值是否存在
if 'training' in request.POST:
或
(无论如何,你不应该和真的比较。)我也试过
False
!我使用了您的第一个示例,非常感谢。我也尝试了False
!我用了你的第一个例子,非常感谢。