Hi all, please I’m getting the this error when I render ResultModelForm in the template.
Here is models.py
class Student(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
profile_pic = models.ImageField(upload_to=student_image, verbose_name=“ProfilePicture”, blank=True)
level = models.CharField(max_length=50)
guardian_email = models.EmailField()
guardian_phone = models.IntegerField()
def __str__(self):
return self.first_name
def get_absolute_url(self):
#return f"/polls/{ self.id }/" static
return reverse("studentportal:student_detail", kwargs={"pk": self.id}) # dynamic
#def __str__(self):
#return "%s - %s" % (self.first_name, self.last_name, self.profile_pic, self.level, self.guardian_email, self.guardian_phone)
class StudentResult(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name=‘myresult’)
english = 'english'
mathematic = 'mathematic'
physic = 'physic'
chemistry = 'chemistry'
computer = 'computer'
agric = 'agric'
subject = [
(english, 'english'),
(mathematic, 'mathematic'),
(physic, 'physic'),
(chemistry, 'chemistry'),
(computer, 'computer'),
(agric, 'agric'),
]
subject = models.CharField(max_length=50, choices=subject)
exam = models.IntegerField()
test = models.IntegerField()
total = models.IntegerField()
def __str__(self):
return self.subject
Forms.py
from django import forms
from .models import Student, StudentResult
class StudentModelForm(forms.ModelForm):
class Meta:
model = Student
fields = (‘first_name’,‘last_name’,‘profile_pic’,‘level’,‘guardian_email’,‘guardian_phone’)
class ResultModelForm(forms.ModelForm):
class Meta:
model = StudentResult
fields = (‘subject’,‘test’,‘exam’,‘total’)
Views.py
class ResultCreateView(CreateView):
template_name = ‘studentportal/result_create.html’
form_class = ResultModelForm
queryset = StudentResult.objects.all() # /_list.html
success_url = ‘/student_result’ #this has override the getAbsoluteUrl in d models
def form_valid(self, form):
print(form.cleaned_data)
return super().form_valid(form)
Here is My template
{% csrf_token %}
{{ form|crispy }}
<button class="regBtn">Submit</button>
<form/>
… Thank you all