Home > Mobile >  Djano UpdateView not rendering modelForm
Djano UpdateView not rendering modelForm

Time:06-25

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

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"
          >
          Save Scores
        </button>
      </form>
   </div>

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

CodePudding user response:

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')
  • Related