Show other contents besides a form in FormView?

I really dislike django CBS design which makes things without flexibility. I would like to have a page whose upper part showing the content of objects and lower part has a form to be posted.

CBS formview

class EditStudent(FormView):
    template_name = "editstudent.html"
    model = models.Student
    success_url = "/home"

How can I retriev objects of studens and show them on the template. Thanks.

'NoneType' object is not callable: I get this error when I don't specify a form class. Created form class 'StForm' associated with model 'Student'. In the EditStudent view class, the CreateView class was inherited, since the data was not saved to the database with the FormView.

Replace bboard with the name of the folder where your templates are placed.
I have this: templates/bboard which are in the application folder.

template_name = 'bboard/tam_form.html'

The success_url row specifies a path based on the path name.

success_url = reverse_lazy('student')

The five most recent records are also transmitted in the context.

context['students'] = Student.objects.order_by('-pk')[:5]

In the template, the first five records are displayed on top and a form is displayed below to fill out.

forms.py

from django.forms import ModelForm
from .models import Student

class StForm(ModelForm):
    class Meta:
        model = Student
        fields = '__all__'

views.py

from .models import Student
from django.views.generic.edit import CreateView
from django.urls import reverse_lazy
from .forms import StForm

class EditStudent(CreateView):
    template_name = 'bboard/editstudent.html'
    form_class = StForm
    success_url = reverse_lazy('student')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['students'] = Student.objects.order_by('-pk')[:5]

        return context

urls.py

from django.urls import path
from .views import *

urlpatterns = [
  path('student/', EditStudent.as_view(), name='student'),
]

editstudent.html

<h4>
{% for aaa in students %}
   <p>{{ aaa }}</p>
{% endfor %}
</h4>
<h2>form</h2>
<form method="post" action="{% url 'student' %}">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="adding">
</form>
Back to Top