How to get started with Django

How to get started with Django

Introduction

As a software engineer, you will encounter many different technologies as you progress in your learning and experience. It's normal that most of them may be unfamiliar to you, especially if you're a beginner.

This lesson is designed to expose you to one of the prominent Python frameworks used in web development, so if you are learning Python programming language and want to advance your learning, this is for you.

Continue reading to learn more about what I will be conveying in the following article.

What is Django

Python's Django high-level web framework enables programmers to quickly build strong and reliable online applications. Since its initial release in 2005, it has grown to be one of the most well-liked web frameworks among Python users.

Model-View-Controller (MVC) architecture pattern is used by Django, which aims to be scalable and maintainable. It comes with a lot of pre-built features, such as an admin interface, a URL routing system, and ORM (Object Relational Mapper).

This enables developers to get going quickly and concentrate on creating the essential components of their online apps.

Some of the key features of Django

The following are the features that you will see in any Django framework that you are to work with.

Built-in ORM

The strong Object-Relational Mapping (ORM) technology offered by Django enables programmers to communicate with databases using Python objects rather than plain SQL. This decreases the likelihood of SQL injection problems and makes it simple to write database queries.

Admin Interface

The admin interface is an essential part of the Django framework and one that I find intriguing when implementing it.

Thanks to Django's built-in admin interface, developers may manage the content of their apps via a web interface. The ability to create, read, update, and remove database objects is provided through this interface.

URL routing

A robust URL routing mechanism in Django enables developers to associate URLs with certain views or functions. This makes handling HTTP requests and building RESTful APIs simple.

Security

Django comes with a number of built-in security capabilities that make securing your online application simple. It offers defense against cross-site scripting (XSS), SQL injection, and CSRF threats.

Scalability

Large volumes of traffic and data can be handled by Django thanks to its scalability design. Many environments, including cloud services like Heroku and AWS, can simply deploy it.

Django is renowned for having a thriving environment and community. There are numerous third-party packages and libraries that may be used to increase Django's functionality. Django Rest Framework, Django CMS, and Django Allauth are a few of the most well-known Django packages.

These features make the Django framework so solid and persuasive for Python developers to employ, and you should always consider it when selecting to design a web app.

If all of the things mentioned above seem to you to be really strange, you shouldn't worry too much because this is normal especially since you are only beginning to use it. As you use it more frequently, everything will begin to make sense.

However, before using the Django framework, you should know most of the stuff outlined below.

Things you should know before working on Django

You must be familiar with the Python programming language to fully understand everything I will teach you in this article for intermediate students. You must also already have a text editor installed on your computer because I won't cover that one in this article.

However, you may download VS Code, my preferred text editor, by clicking this link.

When utilizing the Django framework, web development is also something that is helpful, thus you also need to be well-versed in web development.

You should also be able to use the command line and have a working knowledge of how setups are installed.

Creating a virtual environment

In order to keep all the packages for the project you are working on separate from any other project, the first step in using the Django framework is to build a virtual environment.

Due to the fact that Django and Python both have various versions, you should keep each project distinct to avoid conflicts.

Thus, before you can establish the virtual environment, you must first install a package named virtualenv. If you are using Windows, execute this command to install the virtualenv.

pip install virtualenv

and if you are using Mac or Linus, then run this command.

pip3 install virtualenv

It's time to build your virtual environment for your Django project now that you've finished installing the virtualenv package.

Use the following command to create a virtual environment on Windows..

virtualenv harun_env

The virtual environment that I want to build is known by the title harun_env, though you are free to give it another name.

You must execute this command on your terminal if you are running Mac or Linus.

python3 -m venv ~/harun_env

Now that you are done creating your virtual environment, the next step is to activate your virtual environment and to activate it run the following commands.

For Windows run

harun_env/scripts/activate

And for Mac or Linus run in your command line

source harun_env/bin/activate

The name of your virtualenv will now appear first in brackets in your command line, as seen below, indicating that your virtual environment is active.

(harun_env)

As a result, you are currently in your virtual environment, and you must run this every time you wish to leave it.

deactivate

This takes you from the virtual environment to the command line of the local system.

Now that you have everything up and running it's time to install Django, this means that in order to continue working on your Django project, you must install the Django package in your virtual environment.

How to install Django

Even if you already have the package installed on your local machine, you still need to install it on the newly constructed virtual environment in order to start a project in Django.

Make sure you are in your virtual environment before running this command to install Django.

pip install django

You can install Django and all of its included packages with this command. After the Django package has been successfully installed, you must start a project in your virtual environment.

Here, you must give your project a name that is distinct from the name of your virtual environment. You may choose to call it something like a variable name that describes the function of your project.

Starting your project

In order to start or create your project this command will help you do that.

django-admin startproject myporfolio

For instance, the project that I'm creating has the name myporfolio, but you may give yours any name you like.

You will see important files like manage.py, setting.py, and urls.py when your project is created. These files are crucial to the success of your project.

After finishing the creation of your project, you can launch the development server to view the interface. Start the development server by using the command below.

python manage.py runserver

After running this command in your virtual environment or text editor, you will see the following output.

Your focus should be on the URL address in the preceding image; to access that specific address, copy and paste the address into your browser and press Enter.

The page will appear as shown below, and this indicates that your Django project is now successfully running.

conclusion

I was able to cover what Django is, its main features, things you should know before working with Django, setting up a virtual environment, installing Django, and how to begin your project in this article.

In order for you to get the most out of my writing, I hope this wasn't a waste of your time. With that in mind, please feel free to share this post with anyone you believe will find it useful. Also, don't forget to sign up for my newsletter to receive notifications whenever I post a new article.

Additionally, you can follow me on Twitter. You should always Dm me if you need any form of tech support.