Skip to content

Adding message to center views

In this video, we are going to learn how to use message framework in django.

Resources:

Project Setup

First of all, download the Section 11 Project Code files from the resources section.

  • Now, extract the folder on the desktop.

  • Then, copy the mysite folder which is the project root directory.

  • Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.

  • Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.

  • Then, migrate the changes to the SQLite database.

  • Now, create a superuser having username "admin" and password "abde@12345".

Templates

Now, to use message framework, at first you need to create a component named message.html in the componenet folder.

In this html file, we need to write the code for showing messages to the user. When you browse the django documentation, the code samples is already provided. I have also attched a message componenet html file. Download it and copy its content in the html file.

{% if messages %}
  <ul class="messages">
    {% for message in messages %}
    <li {% if message.tags %} class="{{ message.tags }}" {% endif %}>
      {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}{% endif %}
      <h5>{{ message }}</h5>
      </li>
      {% endfor %}
  </ul>
{% endif %}

In this html code, we are simply iterating over messages and showing those messages in the h5 tag. You don't need to remember this code. You can get it easily from the django documentation.

Now, open your base.html file and include this component.

[After header and before container, include the component]

{% include 'components/message.html' %}

Now, everytime this base.html file is used, django will check for any messages. If any message is found, it will display those message.

Create View

Now, open your views.py file of center.

At first, we need to import the messages from django.contrib

from django.contrib import messages

Now, in the create center view, we will add two different messages.

When the form data is valid, and when the form is saved, we will show success message.

messages.success(request, "Vaccination Center Created Successfully")

So, after saving the form, when the user gets redirected to the center list page, the user will see a message saying that Vaccination Center Created Successfully.

Similarly, if the form data is not valid, we will show error message to the user.

messages.error(request, "Please enter valid data in the form")

So, if the form is invalid, the user will be redirected to the create center page along with one time message saying that Please enter valid data in the form.

[Run the developmenet server and see the changes.]

Update View

Now, lets add success and error message in the center update view.

When the form data is valid and when the form is saved, we will show a success message.

messages.success(request, "Center Updated Successfully")

When the form data is invalid and when the user is being redirected to the same update center page, we will show an error message.

messages.error(request, "Please enter valid data in the form")

[Run the developmenet server and see the changes.]

Delete View

At last, lets add success message to the delete view.

When the delete operation is executed successfully and when the user is redirected to the center list page, we will show a success message.

messages.success(request, "Vaccination Center Deleted Successfully")

[Run the developmenet server and see the changes.]

In this way, we have learnt how to use message framework in function based view.