Build a Django CRUD Application | Django Tutorials
Build a Django CRUD Application
In this article, you will learn how to build a Django CRUD Application step by step. We have shared each and everything related to building CRUD applications in detail. Sure that, if you read this complete article you will get techniques and easy methods to build Django CRUD application properly. Let’s learn!
What is a CRUD application?
It is very common as an Important application to develop for beginners if you take an interest in the development field. CRUD means Create, Read, Update and Delete. Almost every websites or application have these features but beginners don’t know. They understood that it is a new thing but we used such functionality in every application.
It is the application in which user can create post/article/any media like pictures, video or audio, etc, it user like, can view how it is looking bad or good, or if the user wants to make any updation or editing such application allow to update content, and if the user wants to delete any created content, then application allows a user to delete. Such functionality like a website is called a CRUD application.
Why do need to create CRUD applications for beginners?
As we have mentioned that almost every application has a CRUD functionality, if beginners develop a CRUD application, then it will be good for beginners to work with any kind of application. If you learn any web frameworks, CMS, system, MVT, MVC, you will be given an assignment to build a CRUD application in that technology. If you cannot develop a CRUD application, it means, you did not learn anything.
But in this article we will learn how to build a Django CRUD application, that is very easy to develop. So in the following section, we will share the complete source code with an explanation step by step, which makes it easy for beginners to understand.
Steps to create Django CRUD Application
To create a Django application that performs CRUD operations, follow the following steps.
1. Start a Django Project
The very first step is to start a project in which you will create multiple applications. To start a Django project run the following command in the visual studio command prompt or other Ediot you are using.
django-admin startproject school
2. Start an App
After creating a project you have to create an application in which you will add templates, static folders, other functionalities like creating Django forms, models. etc. So run the following command to create an application in your Django project.
python manage.py startapp student
3. Include App in Installed App
This step is very important, sometimes beginners forget this step. When you install any application, libraries, packages you have to include them in the Installed apps list to inform Django. So you also need to add your application to the INSTALLED_APPS list.
You have to open the settings.py file of your project folder and add your application name with quotes as mentioned at the last.
# Application definition for Django CRUD Application
INSTALLED_APPS = [
4. Project Structure
When you run a command for creating a project you will get a number of by default files as asgi.py, wsgi.py, urls.py, settings.py,__init__.py. You also get default files when you start an app as models.py views.py, tests.py, admin.py, apps.py __init__.py. In the following, you will look at the screenshot that is showing our complete project structure.
5. Create a Model
Model is used to define a project organization’s entity. The entity is the real world thing like the place, person, things, concepts or events, etc. So car, student, table, mobile, fan, etc are examples of an entity. When we create a model class, it converts to a database table in the database using commands.
// In your project application folder, models.py file
from django.db import models
s_name = models.CharField(max_length=100)
s_email = models.EmailField()
s_subject = models.CharField(max_length=100)
s_contact = models.CharField(max_length=100)
6. Make Migrations and Migrate
After creating a Django model class you need to run a command which generates a SQL query in order to make a table according to your model class descriptions. So to migration, use the following command:
python manage.py makemigrations
After doing this you need to run another command, which process of applyment on migrations file. After runnign following command a tabel wil be created in the databse.
python manage.py migrate
7. Create a ModelForm
// in your project application folder forms.py file
The template is the collection of HTML, CSS JS, and other frontend skills, in which your content is displayed to the user. You have to create a template folder inside the application folder with the name of the application name as ‘student’. Then create the required files as I have mentioned.
If you want to learn more about the Django template, then we have published a complete Django tutorial on it. Click on the linked page.
You need to create following tempalte to get proper work with us.
# create a html file with name ‘index’ and paste below code
We are ending to building Django CRUD Application, but one step if have to do. Without this step, CRUD operation will be a complete app but you may need static file management. Static files are the important file for a website or web applications as CSS, JS, JQ, or other related fiels. You have to properly manage these files. We have published a complete article on Django static files management, you can read that article to get more information.
So, you have done everything, now you have to run your server with this project. If you want to know about how to run a server for your Django project in detail. We have published a complete article on it.
So using the following command you can run a server at port 8000
python manage.py runserver
After running the server, you will be given a URL, visiting that you can view your Django website:
Now! what you have to do?
As you have read this article about “build a Django CRUD application”, now you must follow instructions as discussed in this article. In case of any issues or problems, don’t worry, you can discuss them with us.
On other hand, If you have any suggestions or knowledge about this article, you can share it with us, we will appreciate you!