Building a Simple Application with Python

Jhon M. Murillo C.
Condor Labs Engineering
4 min readAug 20, 2020
Django python

Python is one of the most popular programming languages in the world since it offers different uses, easy syntax and a low learning curve compared with the other programming languages.

In this post, we’ll see how to do a simple Application with Python using the Django Framework.

When you start in the world of python, a frequent question asked is: What are the proper applications of this programming language? I can tell you that it has many scenarios, but the most popular are:

  • Machine Learning and Artificial Intelligence with libraries like Pandas, Scikit-Learn, and NumPy.
  • Data Science and Data Visualization(Pandas, NumPy), Also Matplotlib and Seaborn for visualization.
  • Desktop GUI with the Tkinter library.
  • Web Scraping Applications with Selenium or BeautifulSoup.
  • Web Development

For Web Development, the most well-known frameworks are Django and Flask. These frameworks provide developers with several tools that reduce development time and complexity, starting from an easy ORM to more difficult tools as Auth.

Prerequisites

let’s install Django and create our project:

mkdir django-project
cd django-project
pip install Django

After installing Django, we’ll proceed to create the App.

django-admin startproject course_crud
Fig 1. scaffolding of the project.

Finally, we can start our app within our project running the next command in the course_crud folder:

python manage.py runserver

Like a result we get this:

Fig 2. Initial page Django server deployed

This is the scaffolding of our project. Now we’ll create the course application.

python manage.py startapp courses_app

This command creates a new folder called course_app with the necessary files.

Fig 3. Course Application Django.

Django provides an ORM and a SQLite Database. We can check the database config in the file course_crud/settings.py.

It’s very important to add each application in the course_crud/settings.py file for the proper operation.

In the INSTALLED_APPS we can add the same name that is in the course_crud/courses_app/apps.py.

Now, we’re ready to start building.

First, let’s create our model in course_crud/courses_app/models.py.

In the course_crud/courses_app/urls.py file include the following code (if the file does not exist, please create it) :

To able be to make requests to courses_app URLs, we need to add the courses_app path in the course_crud/urls.py as follows.

In the course_crud/courses_app/views.py file will be the logic to connect to the database models, do operations, and return an HTTP response.

We’ll create the course list view.

We need to update the code in course_crud/courses_app/urls.py file to call the correct view.

run the server again with the command:

python manage.py runserver

And get the next output:

Fig 4. python manage.py runserver output.

Our server is running but we have to perform the migrations. Migrations refer to changes in the models that have not yet been applied to the database. If we remember, we added the Course Model and we should apply it in the database.

To solve this, we run the following commands:

python manage.py makemigrations
python manage.py migrate
Fig 5. Migrations output

Then we run the server again.

python manage.py runserver
Fig 6. python manage.py runserver output after migrations

We no longer get the error message related to migrations.

We open the Postman App and we make the request http://127.0.0.1:8000/course/.

Fig 7. Course list request without data

The response is a Status code 200, but without data. For this, we’ll add the Course Model to the Admin interface that provides a visual interface to do the Crud Actions.

First, we add this line in course_crud/courses_app/admin.py.

After that, we’ll create a superuser to login to the Admin interface running the next command.

python manage.py createsuperuser
Fig 8. python manage.py createsuperuser

When the superuser is created we enter the link http://127.0.0.1:8000/admin/login/?next=/admin/

Fig 9. Django Login Admin

We log in with our credentials and we have access to all applications and models.

Fig 10. Django admin interfaces initial page.

Let’s proceed to create one or more courses.

Fig 11. Course add interface.
Fig 12. Course List interface.

Now execute the endpoint http://127.0.0.1:8000/course/ again and we get the course list.

Fig 13. Course list with data

References

Thanks for reading, here you can get the source code, download it, check it, make changes, send me your PR, please feel free leave any feedback or questions in the comments 🤘.

--

--