SimpleTuts.com

Understanding Django Models

So far in this tutorial, the output has been static data from Python or HTML templates. Now we will see how Django allows us to work with data without changing or uploading data files.

In Django, data is created in objects called models, which are actually tables in a database.

Step 1

Open the file models.py and create a model named Contact in it

from django.db import models
    
class Contact(models.Model):
    contact_name = models.CharField(max_length=255)
    contact_email = models.EmailField(max_length=255)
    contact_msg = models.TextField()

    def __str__(self):
        return self.contact_name

Step 2

After converting the created model to SQL, give the migration comments so that the table is created in the database.

python manage.py makemigrations
python manage.py migrate

Step 3

Create the HTML form to receive data from the user. Let's link bootstrap framework in master page

{% load static %}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" 
        rel="stylesheet" >
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" 
        ></script>
        <link rel="stylesheet" href="{% static 'mystyle.css' %}">
        <title>{% block title %}{% endblock %}</title>
    </head>
    <body>
        <nav>
        	<a href="/">Home</a>
        	<a href="/about">About</a>
        	<a href="/contact">Contact</a>
        </nav>
        {% block content %}{% endblock %}
        <script src="{% static 'myscript.js' %}"></script>
    </body>
</html>

Step 4

Add a form inside the content block of contact template

{% extends 'master.html' %}
{% load static %}
{% block title %}Contact Page{% endblock %}
{% block content %}
<div class="container">
    <div class="row">
        <div class="col-sm-4 offset-sm-4">
            <div class="bg-secondary text-white p-3 mt-5">
                <h1>CONTACT</h1>
                <form action="/contact" method="post">
                    {% csrf_token %}
                    <div class="mb-3 mt-3">
                    <label for="name" class="form-label">Name:</label>
                    <input type="text" class="form-control" id="name" 
                    placeholder="Enter your name" name="contact_name">
                    </div>
                    <div class="mb-3 mt-3">
                    <label for="email" class="form-label">Email:</label>
                    <input type="email" class="form-control" id="email" 
                    placeholder="Enter your email" name="contact_email">
                    </div>
                    <div class="mb-3 mt-3">
                    <label for="msg">Message:</label>
                    <textarea class="form-control" rows="5" id="msg" 
                    name="contact_msg"></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary">Submit</button>
              </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

Step 5

Your contact form will look like this:-

Step 6

After opening the view, import the contact model

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from . models import Contact

Step 7

Collect the posted form data inside the contact view. Then store that data into the database

def contact(request):

    if request.method == 'POST':
        con_name = request.POST['contact_name']
        con_email = request.POST['contact_email']
        con_msg = request.POST['contact_msg']
    
        contact = Contact.objects.create(
            contact_name=con_name,
            contact_email=con_email,
            contact_msg=con_msg)
        contact.save()
    
    template = loader.get_template("contact.html")
    return HttpResponse(template.render({},request))

After that when you submit the contact form, the data submitted will be stored in the database.