Sitemap
Lavu Sravya

Lavu Sravya

Django —Getting Started

3 min readFeb 3, 2023

--

Django is a high-level Python Web framework that makes it easier to create web sites using Python. It takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. Django emphasizes reusability of components (Don’t Repeat Yourself) and comes with ready-to-use features like login system, database connection & CRUD operations.

How does Django Work?

Django follows the MVT design pattern (Model View Template).

  • Model : The data you want to present, usually data from a database.
  • View : A request handler that returns the relevant template and content-based on the request from the user.
  • Template : A text file (like an HTML file) containing the layout of the web page with logic on how to display the data.

Installation

To install Django, you must have Python installed, and package manager like PIP (which is included in Python from version 3.4)

To check if your system has Python installed, run this command in the command prompt:

python - -version

If you find that you do not have Python installed on your computer, then you can download it for free from the website: https://www.python.org/

It is suggested to have a dedicated virtual environment (Not mandatory, but it’s highly recommended) for each Django project. If you are just getting started, it’s better to start with the right foot.

pip install virtualenv

python -m virtualenv myenv

This will set up a virtual environment, and create a folder named “myenv” with subfolders and files like Include, Lib, Scripts, pyvenv.cfg

Get Lavu Sravya’s stories in your inbox

Join Medium for free to get updates from this writer.

You must activate the virtual environment every time you open the command prompt to work on your project.

myenv\Scripts\activate.bat

(myenv) C:\Users\YourName>

Now, that we have created a virtual environment, we are ready to install Django. The pip package management system is the preferred method for installing Django.

pip install django

Django will be installed in the Python site-packages/ directory. To check whether Django has been successfully installed or not

(myenv) C:\Users\YourName>django-admin - -version

or Run python on terminal

>>>import django

>>>django.get_version()

Now you are ready to create a Django project in your virtual environment

(myenv) C:\Users\YourName>django-admin startproject ProjectName

After we run the command above, it will generate the base folder structure with ProjectName for a Django project and directory looks like this:

Project Directory

Our initial project structure is composed of five files:

  • manage.py: A shortcut to use the django-admin command-line utility. It’s used to run management commands related to our project. We will use it to run the development server, run tests, create migrations and much more.
  • __init__.py: This empty file tells Python that this folder is a Python package.
  • settings.py: This file contains all the project’s configuration. We will refer to this file all the time!
  • urls.py: This file is responsible for mapping the routes and paths in our project. For example, if you want to show something in the URL /home/, you have to map it here first.
  • wsgi.py: This file is a simple gateway interface used for deployment.

Django comes with a simple web server installed. It’s very convenient during the development, so we don’t have to install anything else to run the project locally.

Now that you have a Django project, you can test it by opening the following URL in a Web browser : http://127.0.0.1:8000 after executing

python manage.py runserver

What’s Next?

We have a Django project!

The next step is to create an app in your project.

Edit: Click here to check next part

--

--