Akshay Gaur

Update View

This view is very similar to the CreateView that we discussed previously here.

Essentially, what we want to do is first create a view where we will have the form preloaded with the values from the our current instance so that we may edit it.

To do that, we open our views.py file in our project and add the following to it:

# import the generic views.
from django.views.generic import TemplateView, ListView, DetailView, CreateView, UpdateView


class StudentUpdateView(UpdateView):
    """
    This class will pre-populate the student's details
    in a form which can then be modified to change
    the student's details.
    Utilizes Django's inbuilt UpdateView Class.
    """
    template_name = 'stud-edit.html'
    model = Student
    form_class = StudentForm

Now, we need to define the url that will render this view for us and we will update our urls.py file for that:

from django.urls import path
from students import views

urlpatterns = [
    path('', views.WelcomeView.as_view(), name='welcome'),
    path('depts/', views.DeptListView.as_view(), name='dept_list'),
    path('depts/<str:pk>/details/', views.DeptDetailView.as_view(), name='dept_details'),
    path('students/', views.StudentListView.as_view(), name='stud_list'),
    path('students/<int:pk>/details/', views.StudentDetailView.as_view(), name='stud_details'),
    path('students/add/', views.StudentAddView.as_view(), name='stud_add'),
    path('students/<int:pk>/edit/', views.StudentUpdateView.as_view(), name='stud_update'),
]

So now, if you want to edit a student, you just need to go to the link 'http://127.0.0.1:8001/students/<id>/edit/'. As an example, when we go to http://127.0.0.1:8001/students/1/edit/ we should see the following:

Editable form for student id 1

If you notice our update view form, you will see that it has the header and footer that we had defined in our previous article. Our 'stud-edit.html' in our 'templates' folder was updated to look like this:

{% extends 'base.html' %}

{% block title %} Add Student {% endblock title %}
{% block content %}
    <h2>Student Add Screen</h2>
    <br><br><br>
	<form method="POST" enctype="multipart/form-data">
		<!-- Add this here -->
		{% csrf_token %}
		{{ form.as_p }}
		<button type="submit"> Save </button>
	</form>
{% endblock content %}

Notice how much simpler our page declaration has become with the use of templates. Now we don't need to worry about any boiler plate in our html files.

But there is a problem here that we haven't delt with so far. If you look at the page's setup, the title you will find that whether we go to "127.0.0.1:8000/students/add/" or "127.0.0.1:8000/students/1/edit/", the page title stays the same (which we set as "Add Student") and so does our heading for the page (which says "Student Add Screen"). So, to make it look better, we change our page to the following:

{% extends 'base.html' %}

{% block title %}
    {%  if object %}
        Update Student
    {%  else %}
        Add Student
    {% endif %}
{% endblock title %}
{% block content %}
    <h2>
        {%  if object %}
            Student Update Screen
        {%  else %}
            Student Add Screen
        {% endif %}
    </h2>
    <br><br><br>
	<form method="POST" enctype="multipart/form-data">
		<!-- Add this here -->
		{% csrf_token %}
		{{ form.as_p }}
		<button type="submit"> Save </button>
	</form>
{% endblock content %}

Here, we are taking advantage of the fact that if we have a student object to edit (and therefore an UpdateView), there will be an object that will be available for us to use in our form.

So, if we find that this object is available to us (which we check using the if condition) then we assume that this will be an update page and render the titles and heading as such.

If the object is not available to us, we will assume that the page is for adding a student and therefore, render tht title and the heading accordingly.

Using what we have, now, we can see the difference between the titles and headings if we want to add a student vs if we want to update a student: Student add with correct title and headings. Student update with correct title and headings.

Next, we will take a look at the delete view.