一段时间以来,我一直在尝试使单选按钮显示在Django网站上,并且一直遇到问题。我终于找到了有关手动呈现的表单字段的方法,并且一直在使用它。当我显示它们时,便无法选择它们。如果我能够选择它们,那么我似乎永远无法将这些信息推送到模型中。我尝试了很多不同的方法,但是一直空着。我阅读了这篇文章“ How to Render Django Forms Manually”,希望对您有所帮助,但我不确定我是否理解他在做什么和我在做什么之间的区别。如果这是关于不声明值(这是我所理解的),那么我不知道如何以使按钮显示的方式来实现它。另外,当我能够选择单选按钮和POST时,除了没有将其发布到模型之外,我还在控制台内部收到此错误+----------------+-----------+---------------+-------------+
| pokedex_number | name | classfication | abilities |
+----------------+-----------+---------------+-------------+
| 1 | Bulbasaur | Seed Pokemon | Overgrow |
| 1 | Bulbasaur | Seed Pokemon | Chlorophyll |
| 2 | Ivysaur | Seed Pokemon | Overgrow |
| 2 | Ivysaur | Seed Pokemon | Chlorophyll |
| 3 | Venusaur | Seed Pokemon | Overgrow |
| 3 | Venusaur | Seed Pokemon | Chlorophyll |
+----------------+-----------+---------------+-------------+
。
这就是我现在所拥有的。我将尝试对我的尝试进行注释。
models.py
<ul class="errorlist"><li>month<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
forms.py
from django.db import models
class CustomerData(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
npo = models.CharField(max_length=255)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=255)
month = models.CharField(max_length=255)
def __str__(self):
return self.npo
class Ebook(models.Model):
ebook = models.CharField(max_length=255)
def __str__(self):
return self.ebook
views.py
from django.forms import ModelForm
from django import forms
from .models import CustomerData, Ebook
MONTH_CHOICES = [
('January', 'January'),
('February', 'February'),
('March','March'),
('April', 'April'),
('May', 'May'),
('June','June'),
('July', 'July'),
('August', 'August'),
('September','September'),
('October','October'),
('November','November'),
('December','December')
]
class ContactForm(ModelForm):
firstname = forms.CharField(label='First Name:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "Richard"}))
lastname = forms.CharField(label='Last Name:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "Eby"}))
npo = forms.CharField(label='Organization:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "The Coding Recruiter"}))
email = forms.CharField(label='Email:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "email@example.com"}))
phone = forms.CharField(label='Phone:', required=False, widget=forms.TextInput(attrs={"placeholder" : "XXX-XXX-XXXX"}))
month = forms.ChoiceField(label='Month of Renewal/Expiration:', choices=MONTH_CHOICES, widget=forms.RadioSelect(), required=True)
class Meta:
model = CustomerData
fields = ['firstname', 'lastname', 'npo', 'email', 'phone', 'month']
def clean(self):
cleaned_data = super(ContactForm, self).clean()
firstname = cleaned_data.get('firstname')
lastname = cleaned_data.get('lastname')
phone = cleaned_data.get('phone')
email = cleaned_data.get('email')
month = cleaned_data.get('month')
if not firstname and not lastname and not phone and not email and not month:
raise forms.ValidationError('You have to write something!')
EBOOK_CHOICES = [
('Grant Writing', 'Grant Writing'),
('Volunteer Management', 'Volunteer Management')
]
class EbookForm(ModelForm):
ebook = forms.ChoiceField(label='', choices=EBOOK_CHOICES, widget=forms.RadioSelect(), error_messages={'required': 'Ebook Choice Required'})
class Meta:
model = Ebook
fields = ['ebook']
class RawContactForm(forms.Form):
firstname = forms.CharField(label='First Name:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "Richard"}))
lastname = forms.CharField(label='Last Name:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "Eby"}))
npo = forms.CharField(label='Organization:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "The Coding Recruiter"}))
email = forms.CharField(label='Email:', error_messages={'required': '*'}, required=True, widget=forms.TextInput(attrs={"placeholder" : "email@example.com"}))
phone = forms.CharField(label='Phone:', required=False, widget=forms.TextInput(attrs={"placeholder" : "XXX-XXX-XXXX"}))
month = forms.ChoiceField(label='Month of Renewal/Expiration:', choices=MONTH_CHOICES, widget=forms.RadioSelect(), required=True)
index.html
def index(request):
form = ContactForm(request.POST or None)
form2 = EbookForm(request.POST or None)
context = {
'form2' : form2,
'form' : form
}
if request.method == 'POST':
form = ContactForm(request.POST)
context = {
'form2' : form2,
'form' : form
}
if form.is_valid() & form2.is_valid():
subject = 'FaithGuard Confirmation Email'
# Also tried request.POST.get() and a few other things here
fname = form.cleaned_data['firstname']
lname = form.cleaned_data['lastname']
email = form.cleaned_data['email']
ebook = form2.cleaned_data['ebook']
ebook = form2.save()
newcustomer = form.save()
else:
print('Not Valid')
return render(request, 'landingpage/index.html', context)
# This is an alternative approach that I took to try and bypass the is_valid function since it seemed to be causing the issue, or so I think/thought
def index2(request):
form = RawContactForm()
if request.method == "POST":
form = RawContactForm(request.POST or None)
if form.is_valid():
print(form.cleaned_data)
CustomerData.objects.create(**form.cleaned_data)
else:
print(form.errors)
context = {
'form' : form
}
return render(request, 'landingpage/bootstrap.html', context)
答案 0 :(得分:0)
那是因为我正在使用MaterializeCSS。一旦删除它,它就开始工作。为将来的项目所注意,请勿使用Materialize