Home 01. Simple CRUD Django App
Post
Cancel

01. Simple CRUD Django App

Before you start, make sure you have installed Python on your computer. If you haven’t installed it yet, you can download it here.

After installing Python, if you want to use the virtual environment, you can install it using the following command. otherwise you can skip this step. Head over to the create-project section.

1
pip install virtualenv

After installing the virtual environment, you need to create a virtual environment using the following command.

1
virtualenv venv

After creating the virtual environment, you have to activate it using the following command.

1
venv\Scripts\activate

1. Create Project

1
django-admin startproject crud .

2. Create App

1
python manage.py startapp home

The server started successfully. You can access the server at <http://

3. Create Model

1
2
3
4
5
6
7
8
9
10
11
12
13
from django.db import models

# Create your models here.
class Student(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=100)
    phone = models.CharField(max_length=15)
    address = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

4. Create Migration

1
python manage.py makemigrations
1
python manage.py migrate

5. Create Super User

1
python manage.py createsuperuser

6. Register Model

1
2
3
4
5
from django.contrib import admin
from .models import Student

# Register your models here.
admin.site.register(Student)

7. Create View

1
2
3
4
5
6
7
8
9
10
11
from django.shortcuts import render
from .models import Student

# Create your views here.

def home(request):
    students = Student.objects.all()
    context = {
        'students': students
    }
    return render(request, 'home/home.html', context)

8. Create Base Template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple CRUD Django App</title>
</head>
<body>
    <h1>Simple CRUD Django App</h1>
    {% block content %}
    <!-- Content -->
    {% endblock %}
</body>
</html>
```

9. Create Template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
```html
{% extends 'base.html' %}

{% block content %}
{% if students %}
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Address</th>
                <th>Created At</th>
                <th>Updated At</th>
            </tr>
        </thead>
        <tbody>
            {% for student in students %}
            <tr>
                <td>{{ student.name }}</td>
                <td>{{ student.email }}</td>
                <td>{{ student.phone }}</td>
                <td>{{ student.address }}</td>
                <td>{{ student.created_at }}</td>
                <td>{{ student.updated_at }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
{% else %}
    <p>No data found.</p>
{% endif %}
{% endblock %}
```

9. Create project URL

1
2
3
4
5
6
7
8
from django.contrib import admin
from django.urls import path
from home import views

urlpatterns = [
    path('', views.home, name='home'),
    path('admin/', admin.site.urls),
]

10. Create App URL

1
2
3
4
5
6
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

11. Run Server

1
python manage.py runserver

12. Create form

form is a built-in Django class for creating HTML forms. It is used to create forms with the database model. It is used to create a form in Django.

1
2
3
4
5
6
7
8
9
10
11
12
13
from django import forms
from .models import Student

class StudentForm(forms.ModelForm):
    class Meta:
        model = Student
        fields = '__all__'
    labels = {
        'name': 'Name',
        'email': 'Email',
        'phone': 'Phone',
        'address': 'Address',
    }

13. Create View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from django.shortcuts import render, redirect
from .models import Student
from .forms import StudentForm

# Create your views here.

def home(request):
    students = Student.objects.all()
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = StudentForm()
    context = {
        'students': students,
        'form': form
    }
    return render(request, 'home/home.html', context)

14. Create Template

1
2
3
4
5
6
7
8
9
10
11
12
13
```html
{% extends 'base.html' %}

{% block content %}
<form action="" method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>
{% endblock %}
```

This post is licensed under CC BY 4.0 by the author.

01. Introduction to Elastic Stack

-