Hello,
I'm working on a application form it has multiple checkboxes,when I submit the form it is showing selected choice isn't a valid choice.
If anyone knows how to use checkboxes in ModelForm plz help me.
Models.py
class Application(models.Model): STATE_CHOICES =( ("1","Alabama"),("2","Alaska"),("3","Arizona"),("4","Arkansas"),("5","California"),("6","Colorado"), ("7","Connecticut"),("8","Delaware"),("9","Florida"),("10","Georgia"),("11","Hawaii"),("12","Idaho"), ("13","Illinois"),("14","Indiana"),("15","Iowa"),("16","Kansas"),("17","Kentucky"),("18","Louisiana"), ("19","Maine"),("20","Maryland"),("21","Massachusetts"),("22","Michigan"),("23","Minnesota"),("24","Mississippi"), ("25","Missouri"),("26","Montana"),("27","Nebraska"),("28","Nevada"),("29","New Hampshire"),("30","New Jersey"), ("31","New Mexico"),("32","New York"),("33","North Carolina"),("34","North Dakota"),("35","Ohio"), ("36","Oklahoma"),("37","Oregon"),("38","Pennsylvania"),("39","Rhode Island"),("40","South Carolina"), ("41","South Dakota"),("42","Tennessee"),("43","Texas"),("44","Utah"),("45","Vermont"),("46","Virginia"), ("47","Washington"),("48","West Virginia"),("49","Wisconsin"),("50","Wyoming"), )
GENDER_CHOICES=( ('male','Male'), ('female','Female'), ) k=( ('yes','Yes'), ('no','No'), ) degree=( ('yes','Yes'), ('no','No'), ) classroom=( ('yes','Yes'), ('no','No'), ) genres1=( ('classical','Classical'), ('rock','Rock'), ('musical Theater','Musical Theater'), ('country','country'), ('blues','Blues'), ('christian Contemporary','Christian Contemporary'), ('gospel','Gospel'), ('other','Other'), )
languages1=( ('english','English'), ('spanish','Spanish'), ('german','German'), ('chinese','Chinese'), ('hindi','Hindi'), ('french','French'), ('russian','Russian'), ('turkish','Turkish'), ) firstname=models.CharField(max_length=50) lastname=models.CharField(max_length=50) Email=models.EmailField() phonenumber=models.CharField(max_length=15) password = models.CharField(max_length=100) confirm_password=models.CharField(max_length=100) address1 = models.CharField(max_length=100) address2 = models.CharField(max_length=100) state = models.CharField(max_length=100,choices=STATE_CHOICES) city = models.CharField(max_length=100) zipcode=models.CharField(max_length=15) DOB=models.DateField() Gender=models.CharField(max_length=50,choices=GENDER_CHOICES) k=models.CharField(max_length=50,choices=k) degree=models.CharField(max_length=50,choices=degree) classroom=models.CharField(max_length=50,choices=classroom) genres=models.CharField(max_length=50, choices=genres1) languages=models.CharField(max_length=50, choices=languages1) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) application_field = models.SlugField(max_length = 200)
def __str__(self): return self.firstname
..
Views.py
from django.shortcuts import render,redirect, get_object_or_404from onlinetutors.models import Category,tutorsfrom .forms import applicationfrom .models import Application
Create your views here.
def registration(request): if request.method == 'POST': form=application(request.POST) if form.is_valid(): form.save() print("Hellooooooooo") firstname=form.cleaned_data['firstname'] lastname=form.cleaned_data['lastname'] Email=form.cleaned_data['Email'] phonenumber=form.cleaned_data['phonenumber'] password = form.cleaned_data['password'] confirm_password=form.cleaned_data['confirm_password'] address1 = form.cleaned_data['address1'] address2 = form.cleaned_data['address2'] state = form.cleaned_data['state'] city =form.cleaned_data['city'] zipcode=form.cleaned_data['zipcode'] DOB=form.cleaned_data['DOB'] Gender=form.cleaned_data['Gender'] k=form.cleaned_data['k'] degree=form.cleaned_data['degree'] classroom=form.cleaned_data['classroom'] genres=form.cleaned_data['genres'] languages=form.cleaned_data['languages'] form=Application(firstname=firstname,lastname=lastname,Email= Email,phonenumber=phonenumber, password=password,address1=address1,address2=address2, state= state,city=city,zipcode=zipcode, DOB=DOB,Gender=Gender,k=k,degree=degree,classroom=classroom,languages=languages,genres=genres) form.save() return redirect("/thankyou/") else: form=application() return render(request,"registration.html",{"form":form})
from django import formsfrom django.forms import ModelFormfrom .models import practiceTable
class application(ModelForm): GENDER_CHOICES=( ('male','Male'), ('female','Female'), ) k=( ('yes','Yes'), ('no','No'), ) degree=( ('yes','Yes'), ('no','No'), ) classroom=( ('yes','Yes'), ('no','No'), ) genres1=( ('classical','Classical'), ('rock','Rock'), ('musical Theater','Musical Theater'), ('country','country'), ('blues','Blues'), ('christian Contemporary','Christian Contemporary'), ('gospel','Gospel'), ('other','Other'), )
languages1=( ('english','English'), ('spanish','Spanish'), ('german','German'), ('chinese','Chinese'), ('hindi','Hindi'), ('french','French'), ('russian','Russian'), ('turkish','Turkish'), ) DOB=forms.DateField(widget = forms.SelectDateWidget) Gender= forms.CharField(label='Gender',widget=forms.RadioSelect(choices=GENDER_CHOICES)) k12=forms.CharField(label="Have you taught in K-12 schools?",widget=forms.RadioSelect(choices=k)) degree=forms.CharField(label="Do you have a 4 year degree?",widget=forms.RadioSelect(choices=degree)) classroom=forms.CharField(label="Have you taught in a classroom setting?",widget=forms.RadioSelect(choices=classroom)) genres=forms.MultipleChoiceField(required=True,widget=forms.CheckboxSelectMultiple,choices=genres1) languages=forms.MultipleChoiceField(required=True,widget=forms.CheckboxSelectMultiple,choices=languages1) class Meta: model=Application exclude = ('application_field',)
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/733ce94e-9cd9-4d44-a7e1-140e4245ff10%40googlegroups.com.
No comments:
Post a Comment