I’ve been meaning to write a series on Django which is a web application framework written in Python. To follow this tutorial you don’t need to be a pro in python and have to know it inside-out. Just some basics will get you through it.
Before we start writing applications, we must know a little about what is Django. Django is a web application framework that acts more of an MTV pattern instead of MVC. Think it this way:
Model remains model
View has been replaced by Templates
Controller gets replaced by View
A simple Hello-world application is just a few lines of code in Django! But moving from this simple thing a full fledged application can be a daunting task in itself. There are other concepts that can help proceed further such as ORM, migrations etc that help in building a bigger application. But for this tutorial we’ll be building a simple CRUD( Create, Retrieve, Update and Delete ) application.
To get started with you need to have python and virtualenv installed on your machine. Python is already installed on the linux systems. But you'll need to install virtualenv. To install virtualenv follow the command:
Before we actually start writing code we need to get a hold of the application structure. We'll first execute several commands that are essential in django project development.
After installing virtualenv, we need to set the environment up.
We are setting a virtual environment of the name venv here. Now we need to activate it.
Now that it has been activated. We need to start our project. Feed in the following command to start a project.
The first line installs Django v1.11.8 and creates a directory named app in the parent directory. the third line starts a project named crudapp in the app directory. The directory tree should look like
We'll see the meaning of each file and what it does one by one. But first, to test if you are going in the right directoion, run the following command.
If you get an output like below then you're doing it right.
Let's see what exactly the different files that we created mean.
__init__.py: Acts as an entry point for your python project.
settings.py: Describes the configuration of your Django installation and lets Django know which settings are available.
urls.py: used to route and map URLs to their
wsgi.py: contains the configuration for the Web Server Gateway Interface. The Web Server Gateway Interface (WSGI) is the Python platform standard for the deployment of web servers and applications.
Writing the Application
Now this is where we start coding our app. For this operation we'll consider blog post as our entity. We'll be applying CRUD operations to blog posts.
The app in our project will be called blog_posts.
This will create the necessary files that we require.
First and foremost create the Model of our application.
Now that we have our model ready, we'll need to migrate it to the database.
Now we create our Views where we define each of our CRUD definition.
Now that we have our Views, we create mappings to URL in our
/crudapp/blog_posts/urls.py file. Make a note that the following is our app specific mappings.
Now we create project specific mappings in
Now almost everything is done and all we need to do is create our templates to test the operations.
Go ahead and create a
templates/blog_posts directory in
Now we have all the necessary files and code that we require.
The final project tree should look like following:
python manage.py runserver and voila!! You have your Django app ready.
Leave a Reply
Your email address will not be published. Required fields are marked *