Top 9 Django Concepts - Part 2 : 5 Mins

Introduction

undraw ideas s70l

Welcome to part 2 of the 3 part series for Top 9 Django Concepts.

I will be covering 3 Django concepts, for those who had missed the first part of the 3 part series, you can head down to the Top 9 Django Concepts - Part 1

The first concept is essential Django commands that you will be using when developing in Django.

The second is the concept of using either a front-end like Vue, React or Angular web framework or using Django existing template system to build UI.

Lastly, it will be to use either Class-Based Views vs Function-Based Views.

Essential Django Commands

undraw progress tracking 7hvk

So here are some of the basic commands you will use when you are developing in Django.

Creating a new Django Project

undraw all the data h4ki

This is the first command that you will encounter when you are creating a Django project for the first time.

django-admin startproject project-name

This command helps to create a template project folder with all the standard list of files or folders that are required for Django to run.

The folder with the same name as the project, contains configuration setting, url routing and serving configuration.

For further explanation of the default project structure of the template project.

Please head down to Create a new project in the Django documentation.

Starting the Django Web Server

undraw start building vqhd

By default, all Django projects are shipped with a development web server.

This is used to test out your website before you deploy it in a production environment using a production-level web server like Gunicorn or uWISGI or Mod WISGI.

To start the Django web development server, you have to enter the following command:

python manage.py runserver

By default, Django runs on an IP address of 127.0.0.1 and the port number of 8000.

You could change the IP address and the port number to serve it in a different IP address or port number by entering the following command:

python manage.py runserver  ip_address:port 

Once the development server is up and running, you could leave it without interfering it.

The development server has a built-in function to reload the server whenever a new code is added.

Creating a Django Application

undraw complete design ongo

You can think of Django applications as add-on modules to do a single task for the project which forms the whole website.

Now, you create a new application by entering the following command:

python manage.py startapp appname

This command creates a new Django application with various files and folders to execute the application.

It is always recommended to split multiple features into smaller applications.

Instead of having a single big application which does everything for ease of maintenance and readability of the project.

Creating & Updating Database Tables

undraw personal data 29co

In Django, database tables are represented as a model.

Which could be founded in each Django application's models.py file.

Details on how to create a model could be found on Creating Models in the Django documentation.

Once a model has been created in the models.py.

The developer has to perform 2 commands to make changes to the database.

The first command is called makemigration, which prepares the changes to be made to the database as migration aka version:

python manage.py makemigrations

The second command is called migrate.

Which applies all migrations to the database to either create a new table or update an existing table.

For further details about the migrate or makemigrations command you can go to Creating Models that is founded in the Django documentation

Accessing Django Shell

undraw hacker mind 6y85

By entering the following command, it will open an interactive python shell with all the Django modules loaded.

python manage.py shell

This command is useful when you plan to test out various Django's ORM queries before adding it into your Django App.

Details on how to perform various ORM queries could be founded at Django ORM Querysets By DjangoGirls.

Using Front-end Framework vs Django's Template?

undraw design sprint x3eb

Due to the exploding popularity of Javascript web frameworks that are used for front-end development.

You may encounter this dilemma when working with clients or in your work.

Ultimately, this comes down to the following list of questions I will ask myself:

  • What are the project requirements?
  • Who is in the project? Is it yourself or a team of developers that includes a front-end developer?
  • What is your experience in working with Vue, React or Angular?

If the project does not require you to work with front-end web frameworks like Vue, React or Angular.

I suggest that you stick with Django's default template system and integrate it with prebuilt bootstrap themes or bootstrap examples to speed up the development process.

If your work requires Django to be a backend system.

You should start to look at building RESTful APIs using Django REST Framework to support the front-end developer with your APIs.

Class-Based View vs Function-Based View

undraw share opinion jpw0

There is a Debate between the use of Class-Based Views (CBV) or Function-Based Views (FBV).

Due to the reason that both have their pros and cons.

I would suggest you use Class-based Views only when you understand it and found the use case for using it in your project.

Which is the need to reuse and customising purposes in your project or else use Function-based View.

If you are a beginner in Django due to it's simplicity and straight forward way of presenting the logic in your code.

Conclusion

undraw review fkgn

I hope that you gain a better understanding of various concepts that you may encounter as a Django developer.

These concepts are design or development decisions.

Which you might need to make due to the depth of knowledge required and Django's role that increasingly being used as a backend system.

References