Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Djano UpdateView not rendering modelForm

I have a model assesment which is related to other models.
i have created a UpdateView, Form based on the model assesment.
The problem now is when I render the form in a template, no field is displayed except the submit button, so there is nothing to update.
i only see a submit button with no form fields

enter image description here

Here designs below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

models.py

class assessment(models.Model):
className = models.ForeignKey(all_class, on_delete=models.SET_NULL, null=True)
student = models.ForeignKey(students, on_delete=models.SET_NULL, null=True)
subjectName = models.ForeignKey(allsubject, on_delete=models.SET_NULL, null=True)
firstCa = models.IntegerField(default=0)
secondCa = models.IntegerField(default=0)
exam = models.IntegerField(default=0)
section = models.CharField(max_length=100, choices=section_choices)
term = models.CharField(max_length=100 , choices=term_choices)
session = models.CharField(max_length=1000)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
    return str(self.className)
class Meta:
    ordering = ['className']

views.py

class assessmentEntry(UpdateView):
model = assessment
fields = '__all__'
Form_class = AssessmentForm
template_name = 'result/assessmentScore.html'
success_url = reverse_lazy('result:index')

Forms.py

class AssessmentForm(forms.ModelForm):
class Meta:
    model = assessment
    fields = ['className','student','subjectName','firstCa','secondCa','exam']

urls.py

 path('assessmentScores/<int:pk>/', assessmentEntry.as_view(), name='assessmentScores'),

template(assessmentScore.html)

<div >
    <form method="POST">
      {% csrf_token %}
        {{ Form.as_p}}
        <button
          type="submit"
          class="px-2 text-white bg-indigo-500 rounded-md  focus:bg-indigo-600 focus:outline-none">
          Save Scores
        </button>
      </form>
   </div>

Please what exactly am I doing wrong? and how do I correct it.

>Solution :

Class-based views that inherit from the FormMixin [Django-doc] like a FormView, CreateView, UpdateView and DeleteView pass the form object to the template as form, not Form, so you render this as:

{{ form.as_p }}

Note: While not necessary, usually URLs are written in kebab case, so:

path('assessment-scores/<int:pk>/', assessmentEntry.as_view(), name='assessmentScores')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading