DEV Community

Cover image for How to Install Django and Create Your First Django Project | Django Tutorial #2
S.P.Randhawa
S.P.Randhawa

Posted on

How to Install Django and Create Your First Django Project | Django Tutorial #2

How to Install Django and Create Your First Django Project | Django Tutorial #2

If you have recently started learning Django, you might be wondering how to set up your development environment and create your first Django project.

In my previous article, we explored what Django is, its features, architecture, and applications.

Now, it's time to move from theory to practice!

In this tutorial, we will learn how to install Django, create a virtual environment, set up our first project, and run a simple Django application.

Let's get started! 🚀

1. Prerequisites

Before installing Django, make sure you have the following:

  • Basic knowledge of Python.
  • Python installed on your computer.
  • A code editor such as Visual Studio Code.
  • Basic knowledge of using the terminal or command prompt.

To check whether Python is installed, open your terminal and execute:

python --version
Enter fullscreen mode Exit fullscreen mode

If Python is installed correctly, you will see its version.

2. Create a Project Folder

First, create a folder where you want to store your Django project.

For example:

mkdir DjangoTutorial
cd DjangoTutorial
Enter fullscreen mode Exit fullscreen mode

This folder will contain all the files related to our project.

3. Create a Virtual Environment

A virtual environment is an isolated Python environment that allows us to manage project-specific packages separately.

It helps prevent dependency conflicts between different Python projects.

Create a virtual environment using:

python -m venv venv
Enter fullscreen mode Exit fullscreen mode

To activate it on Windows PowerShell:

.\venv\Scripts\Activate
Enter fullscreen mode Exit fullscreen mode

After activation, you will notice (venv) appearing before your terminal path.

4. Install Django

Now that our virtual environment is activated, let's install Django using pip.

python -m pip install django
Enter fullscreen mode Exit fullscreen mode

To verify the installation, execute:

python -m django --version
Enter fullscreen mode Exit fullscreen mode

If Django is installed successfully, its version will appear on your screen.

Congratulations! Django is now ready to use.

5. Create Your First Django Project

A Django project is the main configuration and structure of a web application.

To create a project, execute:

django-admin startproject myproject
Enter fullscreen mode Exit fullscreen mode

This command automatically generates the basic files and folders required for a Django project.

Now, move into your project directory:

cd myproject
Enter fullscreen mode Exit fullscreen mode

6. Understand the Django Project Structure

After creating your project, you will find the following structure:

myproject/
│
├── manage.py
│
└── myproject/
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
Enter fullscreen mode Exit fullscreen mode

Let's understand the important files:

1. manage.py

It is a command-line utility used to perform various Django tasks, such as running the development server and creating applications.

2. settings.py

This file contains the main configuration settings of the project, including installed applications, database configuration, middleware, and templates.

3. urls.py

It defines the URL patterns that connect incoming requests to the appropriate views.

4. asgi.py

It provides the entry point for ASGI-compatible web servers.

5. wsgi.py

It provides the entry point for WSGI-compatible web servers.

6. init.py

It tells Python that the directory should be treated as a Python package.

7. Apply Initial Database Migrations

Django comes with built-in applications that require database tables.

To create these tables, execute:

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Django will apply the initial database migrations using its default SQLite database configuration.

8. Run Your First Django Server

Now comes the exciting part!

To start the development server, execute:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

You will see a message indicating that the development server is running.

Open your browser and visit:

http://127.0.0.1:8000/

If everything is configured correctly, you will see the default Django welcome page.

🎉 Congratulations! You have successfully created and run your first Django project.

Note: Django's development server is intended for local development and testing, not production hosting.

9. Create Your First Django Application

A Django project can contain multiple applications.

For example, an e-commerce project might have separate applications for:

  • Users and authentication
  • Products
  • Shopping cart
  • Orders and payments

Let's create a simple application named core.

Stop the development server by pressing CTRL + C and execute:

python manage.py startapp core
Enter fullscreen mode Exit fullscreen mode

This command creates a new application with the following structure:

core/
│
├── migrations/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py
Enter fullscreen mode Exit fullscreen mode

Some important files include:

  • admin.py: Used to register models with Django's admin interface.
  • apps.py: Contains application configuration.
  • models.py: Defines the database models.
  • views.py: Contains the logic that handles requests and returns responses.
  • tests.py: Used to write automated tests.
  • migrations/: Stores database migration files.

10. Create Your First Django View

Let's create a simple webpage that displays a welcome message.

Open core/views.py and write the following code:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello! Welcome to my first Django project.")
Enter fullscreen mode Exit fullscreen mode

Here:

  • HttpResponse is used to return an HTTP response.
  • home() is a view function.
  • request contains information about the incoming HTTP request.

11. Connect the URL to Your View

Open the project's urls.py file and update it as follows:

from django.contrib import admin
from django.urls import path
from core import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
]
Enter fullscreen mode Exit fullscreen mode

Here, the empty URL path ('') represents the homepage.

When someone visits the homepage, Django calls the home view.

12. Register Your Application

Open settings.py and locate the INSTALLED_APPS list.

Add your application name:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'core',
]
Enter fullscreen mode Exit fullscreen mode

This tells Django that our core application is part of the project.

13. Run the Project Again

Now execute:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Visit:

http://127.0.0.1:8000/

You should now see:

Hello! Welcome to my first Django project.

And that's it! You have successfully created your first Django project, created an application, written a view, and connected it to a URL.

Conclusion

In this tutorial, we learned how to install Django, create a virtual environment, set up a Django project, understand its folder structure, create an application, and display our first webpage.

This is just the beginning of Django development. There are many more interesting concepts to explore, including templates, models, forms, databases, authentication, and deployment.

Keep practising and building projects because practical experience is one of the best ways to improve your development skills.

What's Next?

In Django Tutorial #3, we will explore:

  • What are Django Templates?
  • How to create HTML templates.
  • How to configure the templates directory.
  • How to render HTML pages using Django views.
  • How to use template variables and template tags.

Stay tuned for the next part of this Django tutorial series!


Useful Resources:

Happy Coding! 💻

Written by SP Randhawa

Top comments (1)

Collapse
 
sp_randhawa profile image
S.P.Randhawa •

🚀 Just published Part 2 of my Django Tutorial Series!

In this article, we'll go step by step through installing Django, setting up a virtual environment, creating our first Django project, and running our first application.

If you're a beginner learning Django, I hope this tutorial helps you get started!

💬 Have you created your first Django project yet? Share your experience below!

Happy Coding! 💻