Python Django Tutorial: The Best Guide on Django Framework

A web application development framework is a product structure designed to support web application development, including web services, web resources, and web APIs. Web frameworks provide a standard way to build and deploy web applications on the world wide web. In this article, we will discuss Python Django, which is the most trending framework nowadays.

In this tutorial, we will discuss:

  • What is Python Django?
  • Features of Django
  • Setting up the Django environment
  • Creating your first project
  • What is an app?
  • Creating our view

What Is Python Django?

Python Django is a framework for web applications, as it allows developers to use modules for faster development. As a developer, you can use these modules to create apps and websites from an existing source. It speeds up the development process significantly, as you do not have to code everything from scratch. Using Django, you can create professional web apps and websites in a short window. The platform is known for its advanced functionality like admin panels, authentication support, comment boxes, file upload support, contact forms, app management, and much more.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Features of Django Framework

Some of the key features of Python Django are:

  • It is extremely fast.
  • It has tons of packages that help you in developing your website faster and easier.
  • It is free and open-source.

Setting up the Django Environment

There are certain prerequisites to understand when it comes to Django. Here is a step-by-step outline of the same:

  • Install Python from https://www.python.org/downloads/. After installation, open the terminal by searching for ‘cmd’. In the command line, enter python --version. It will show you the current version of Python installed.
  • Install virtual environment wrapper by typing pip install virtualenvwrapper-win.
  • Now we create a virtual environment by typing mkvirtualenv environment_name. It will create your virtual environment.
  • Install Django by typing pip install Django in the terminal.

Creating your first project

Now you are ready to create your first Python Django project.

The command to create our project is Django-admin startproject project_name. Let us name this project as ‘Djangoproject’.

djano

You will see a folder with the name ‘Djangoproject’ created in the specified path.

Next, we will be using the Sublime text editor to open the files.

If you open your project folder, you will see several files there. Let’s take a look at each of these files.

  • The Init File

This file is empty. The very presence of this file is only to inform you that this package here is not any ordinary folder. It’s a Python package.

  • Settings File

Settings file is an essential configuration file. This file is modified when your application goes into the real world.

  • URL Speech

This file maps the URL of the page to the function of the page, that we will be performing.

  • Wsgi.py

This page contains an application callable which the application server uses to communicate with your code.

  • Manage.py

This is the page used to interact with your project. We never touch the manage.py page.

Before we start, we want to inform you that this tutorial will focus on how Django works. We will not go into details on how our page is displayed.

What Is an App?

Every page in Django is called an app. Every app has its functions. To make an app, write python manage.py startapp app_name in the terminal. Let us name this app, ‘myapp’.

Now, if you look at the ‘myapp’ directory, you will see some extra files related to your back-end database.

Now we have to import our application manually into the project settings. For that, open your Djangoproject/settings.py and add your app manually.

settings_py

Creating Our View

Now it’s time to create your view. Open myapp/views.py

In the above code, you have returned httpresponse. Next up, let’s map this view to your URL.

For that, you have to create a new python file “urls.py” inside your ‘myapp’ folder. In myapp/urls.py include the following code:

The next step is to point the root URL to the myapp.urls. Open your Djangoproject/urls.py file and write the code mentioned below:

urls_py-Django_project

Now let’s run our server. Type python manage.py runserver in your terminal. 

Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.

Conclusion

In this ‘Python Django’ tutorial, we discussed the Django framework and how to set up the Django environment. We also created a small project and executed it in our local machine to view and understand the page that we have created. 

Do you have any more questions regarding Python Django? If you do, please feel free to mention them in our comments section, and our experts will answer them promptly. 

Want to Learn More About Python?

Python is such a popular programming language, and needless to say, Python developers are in super high demand. If you are ready to kickstart your software development career, check out our Python Training Course today!

About the Author

Aryan GuptaAryan Gupta

Aryan is a tech enthusiast who likes to stay updated about trending technologies of today. He is passionate about all things technology, a keen researcher, and writes to inspire. Aside from technology, he is an active football player and a keen enthusiast of the game.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.