Reputation: 105
I'm having problems with a Django form. I cant find the error and how I can fix it.
models.py
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin,
BaseUserManager
from django.core.validators import RegexValidator
from django import forms
class Person(models.Model):
person_userid=models.ForeignKey(User)
person_availability_choices=(
('M', 'Monday'),
('T', 'Tuesday'),
('W', 'Wednesday'),
('Th', 'Thursday'),
('F', 'Friday'),
('Sat', 'Saturday'),
('Sun', 'Sunday'),
)
person_availability=models.CharField(max_length=9, choices=person_availability_choices)
person_salutation_choices=(
('Mr.', 'Mr.'),
('Mrs.', 'Mrs.'),
('Ms.', 'Ms.'),
('Dr.', 'Dr.'),
)
person_salutation=models.CharField(max_length=4, choices=person_salutation_choices)
person_first_name=models.CharField(max_length=50)
person_middle_name=models.CharField(max_length=50)
person_last_name=models.CharField(max_length=50)
person_date_of_birth=models.DateField(null=True, blank=True)
person_address_street=models.CharField(max_length=50)
person_address_suite=models.IntegerField(max_length=10)
person_address_city=models.CharField(max_length=50)
person_address_state=models.CharField(max_length=2)
person_address_zip=models.IntegerField(max_length=10)
person_address_county=models.CharField(max_length=25)
person_live_within_bloom=models.CharField(max_length=1, choices=yes_no)
person_home_phone=models.IntegerField(max_length=10)
person_cell_phone=models.IntegerField(max_length=10)
person_work_phone=models.IntegerField(max_length=10)
person_email=models.CharField(max_length=50)
person_second_email=models.CharField(max_length=50)
person_communication_preference_choices=(
('Phone','Phone'),
('Email', 'Email'),
('Text', 'Text'),
)
person_communication_preference=models.CharField(max_length=10, choices=person_communication_preference_choices)
person_dob=models.DateField()
person_entered_in_donor_database=models.CharField(max_length=1, choices=yes_no)
person_community_member=models.CharField(max_length=1, choices=yes_no)
person_participation_start_date=models.DateField()
person_participation_end_date=models.DateField()
person_reason_for_leaving_choices=(
('Moving', 'Moving'),
('Financial', 'Financial'),
('Medical', 'Medical'),
('Graduated', 'Graduated'),
('CSH', 'Completed Service Hours'),
('Other', 'Other'),
)
person_reason_for_leaving=models.CharField(max_length=20, choices=person_reason_for_leaving_choices)
person_note=models.TextField(max_length=500)
class Volunteer(Person):
volunteer_types=(
('Program', 'Program'),
('Admin', 'Admin'),
('Committee', 'Committee'),
)
person_volunteer_type=models.CharField(max_length=10, choices=volunteer_types)
volunteer_allergies=models.CharField(max_length=100)
volunteer_medications=models.CharField(max_length=100)
volunteer_health_notes=models.CharField(max_length=100)
volunteer_high_school_grad_date=models.DateField(null=True, blank=True) # replace null = True.... with name as done for date_of_birth
volunteer_college_name=models.CharField(max_length=50)
volunteer_college_grad_date=models.DateField(null=True, blank=True)
volunteer_orientation_date=models.DateField(null=True, blank=True)
leader_training1_start_date=models.DateField(null=True, blank=True)
leader_training1_end_date=models.DateField(null=True, blank=True)
leader_training2_start_date=models.DateField(null=True, blank=True)
leader_training2_end_date=models.DateField(null=True, blank=True)
stable_manager_training_start_date=models.DateField(null=True, blank=True)
stable_manager_training_end_date=models.DateField(null=True, blank=True)
volunteer_community_member=models.CharField(max_length=1, choices=yes_no)
volunteer_board_member=models.CharField(max_length=1, choices=yes_no)
volunteer_board_member_start_date=models.DateField(null=True, blank=True)
volunteer_board_member_end_date=models.DateField(null=True, blank=True)
volunteer_horse_experience=models.CharField(max_length=1, choices=yes_no)
volunteer_horse_experience_details=models.CharField(max_length=100)
volunteer_experience_with_disabilities=models.CharField(max_length=1, choices=yes_no)
volunteer_experience_with_disabilities_details=models.CharField(max_length=100)
volunteer_computer_skills_details=models.CharField(max_length=100)
volunteer_facility_maintenance_experience_details=models.CharField(max_length=100)
volunteer_construction=models.CharField(max_length=1, choices=yes_no)
volunteer_lift_fifty_lbs=models.CharField(max_length=1, choices=yes_no)
volunteer_language_proficiency=models.CharField(max_length=50)
volunteer_CPR_trained=models.CharField(max_length=1, choices=yes_no)
volunteer_landscaping=models.CharField(max_length=1, choices=yes_no)
volunteer_skill_fundraising=models.CharField(max_length=1, choices=yes_no)
volunteer_skill_PR=models.CharField(max_length=1, choices=yes_no)
volunteer_skills_other=models.CharField(max_length=500)
volunteer_of_month_recognition=models.DateField(null=True, blank=True)
volunteer_of_year_recognition=models.DateField(null=True, blank=True)
#volunteer_personid=models.ForeignKey(Person)
views.py
def volunteer_form(request):
if request.method == 'POST':
form = VolunteerForm(request.POST)
if form.is_valid():
form.save()
return HttpResponse('<script language="JavaScript"> alert("You have sucessful created a new Volunteer"); location.href="http://127.0.0.1:8000/login/volunteer_form/" </script>')
#return HttpResponseRedirect('/')
else:
detail_id = request.GET['id']
query_results = User.objects.filter(id = detail_id)
form = VolunteerForm()
return render(request, 'loginPortal/volunteer_form.html', {'form' : form, 'query_results' : query_results})
forms.py
from django.db import models
from django.forms import ModelForm
from loginPortal.models import Volunteer, Client
class VolunteerForm(ModelForm):
class Meta:
model = Volunteer
class ClientForm(ModelForm):
class Meta:
model = Client
So I'm getting the error that the form is not validated. and I cant find the error. If any one can help me with this, I will be glad
Upvotes: 1
Views: 227
Reputation: 599460
The last part of your view should look like this:
else:
form = VolunteerForm()
detail_id = request.GET['id']
query_results = User.objects.filter(id = detail_id)
return render(request, 'loginPortal/volunteer_form.html', {'form' : form, 'query_results' : query_results})
Only the form
definition should be inside else
, all the rest should be outside that block.
Upvotes: 1
Reputation: 14311
It forms.py, you're referencing:
model = Client
...after doing an import above. You don't have a model called 'Client' in models.py, you have a model called 'Person'.
Upvotes: 1