Day 2: Getting Started with Django – Create a Welcome Page in 8 steps

Absolutely, let’s walk through the process of starting a Django project, creating an app, setting up a view, adding a URL, configuring settings, applying migrations, and running the development server, all while providing detailed explanations for each step.

Step 1: Starting a New Django Project

  1. Install Django: If you haven’t already, install Django using pip by running the following command:
   pip install Django
  1. Create a New Project: Navigate to the directory where you want to create your Django project and run the following command (replace “myproject” with your desired project name):
   django-admin startproject myproject

Django Project Structure

Django projects follow a well-defined structure, making it easy to organize and manage your web applications. Here’s a basic project structure for a Django project named “myproject”:

markdownCopy codemyproject/
├── myproject/
│   ├──
│   ├──
│   ├──
│   ├──
│   └──
├── myapp/
│   ├──
│   ├──
│   ├──
│   ├── migrations/
│   │   └── ...
│   ├──
│   ├──
│   └──
  • myproject/: This is the main project directory.
    • myproject/: This inner directory is the Python package for your project.
      • An empty file that tells Python this directory should be considered a Python package.
      • Configuration settings for your project.
      • URL routing for your project.
      • ASGI application entry point (for asynchronous servers).
      • WSGI application entry point (for traditional web servers).
  • myapp/: This is an example app within your project.
    • Also an empty file indicating this is a Python package.
    • Configuration for the Django admin interface.
    • App configuration.
    • migrations/: Database migration files.
    • Defines your application’s data models.
    • Unit tests for your app.
    • Defines view functions and logic for your app.
  • A command-line utility for managing your project.

With the project structure in place, let’s explore the core components of Django.

  1. Navigate to the Project Directory: Move into your project directory:
   cd myproject

Step 2: Creating a New App

  1. Create a New App: To create a new app within your project, run the following command (replace “myapp” with your desired app name):
   python startapp myapp
  1. Add the App to Your Project: Open the myproject/ file and add your app to the INSTALLED_APPS list. Add your app name, e.g., ‘myapp’, to the list as follows:

Step 3: Creating a Welcome Page

  1. Create a Template: Inside your app’s directory (myapp), create a folder named ‘templates‘ if it doesn’t already exist. Inside the 'templates' folder, create a file named welcome.html'. This file will be the HTML template for your welcome page. You can use any HTML content you like, but for a basic welcome message, you can use:
   <!DOCTYPE html>
       <title>Welcome to Django World</title>
       <h1>Welcome to the Django Web Framework World!</h1>

Step 4: Creating a View

  1. Create a View: In your app’s directory (e.g., myapp), open the file and create a view function. Here’s an example:
   from django.shortcuts import render

   def welcome_view(request):
       return render(request, 'welcome.html')

In this view function, we import ‘render' from Django, which is used to render HTML templates. The ‘welcome_view' function takes a request as a parameter and renders the ‘welcome.html’ template.

Step 5: Adding a URL

  1. Create a URL Configuration: In your app’s directory (e.g., ‘myapp'), create a' file if it doesn’t already exist. In this file, define the URL pattern for your welcome page:
   from django.urls import path
   from . import views

   urlpatterns = [
       path('welcome/', views.welcome_view, name='welcome'),

This code maps the URL path ‘welcome/’ to the welcome_view function.

  1. Include the App URLs in Project URLs: Open the myproject/' file and include your app’s URL configuration. Add the following line to include your app’s URLs:
   path('myapp/', include('myapp.urls')),

Step 6: Configuring Settings

  1. Static Files Configuration: In your ‘myproject/‘, add the following code to configure static files (replace ‘myapp‘ with your app name):
   STATIC_URL = '/static/'
   STATICFILES_DIRS = [os.path.join(BASE_DIR, 'myapp/static')]

This allows Django to serve static files from your app.

Step 7: Applying Migrations

  1. Migrate the Database: Run the following commands in command prompt to create and apply migrations:
   python makemigrations
   python migrate

This creates the necessary database tables.

Step 8: Starting the Development Server

  1. Start the Development Server: Run the following command to start the development server:
   python runserver

The development server should start on the default port (usually 8000). You can access your welcome page by opening a web browser and navigating to http://localhost:8000/myapp/welcome/.

You’ve successfully created a Django project, added an app, created a welcome page with a view and URL pattern, configured settings, applied migrations, and started the development server. You now have a basic Django application up and running. Explore further to build more complex web applications with Django!

What’s Next?

In Day 3, we’ll take the theory you’ve learned and put it into practice by guiding you through the process of building your first Django application. Get ready to see Django in action and start building web applications from scratch!

  • Day 4
    – Django URL Routing and Creating Django Admin
  • Day 5 – Handling Static Files in Django and Deployment

Previous Blog: If you have not setup your virtual environment for your django project, then refer to Day 1 link:

  • Day 1 – Setting Up Your Python Environment for Django Web Development

With your welcome page up and running, you’ve taken a significant step in your Django journey. In the upcoming days, we’ll delve into more Django features and build more advanced web applications. Stay excited and keep coding!

Stay engaged, keep learning, and remember, each day brings you closer to mastering Django!

Don’t forget to bookmark this page for quick access to your 30-day Django mastery course.

1 thought on “Day 2: Getting Started with Django – Create a Welcome Page in 8 steps”

Leave a Comment