Creating first Project in Django – Getting Started

Creating Your First Django Project: A Journey into Web Development

Django is not just another Python web framework—it’s a gateway to building robust, scalable, and elegant web applications. If you’ve ever wondered how to start your journey with Django, this blog is your definitive guide. By the end, you’ll have your very own Django project running a simple “Hello, World!” web page, but more importantly, you’ll understand the process behind it.

Why Django?

Django simplifies web development by providing tools for every common web development task, from database management to URL routing. It’s fast, secure, and incredibly versatile, making it a favorite for both beginners and seasoned developers.

In this blog, we’ll guide you through the steps to set up your first project, introducing essential concepts along the way.

Setting the Stage: Prerequisites

Before diving in, let’s ensure you have everything you need:

  1. Python Installed: Django runs on Python 3.6 or later. Download it from python.org.
  2. Pip Installed: Pip, Python’s package manager, typically comes bundled with Python.
  3. Virtual Environment (Recommended): Isolating your project dependencies is good practice and avoids version conflicts.

Once you’re ready, let’s dive into the steps.

Step 1: Installing Django

The first step is setting up Django on your system. Open your terminal or command prompt and install Django using pip:

				
					pip install django
				
			

After installation, confirm it by checking the version:

				
					django-admin --version
				
			

You’re now equipped with Django, ready to create your first project.

Step 2: Building the Project Foundation

Every Django application begins as a project. Create one with the following

				
					django-admin startproject myproject
				
			

Your project directory should look like this:

myproject/

|– manage.py

myproject/

|– manage.py

|– myproject/

    |– __init__.py

    |– settings.py

    |– urls.py

    |– asgi.py

    |– wsgi.py

  • manage.py: A script for managing your project.
  • settings.py: Contains your project’s configuration.
  • urls.py: Maps URLs to views, acting as your project’s navigator.

Move into the project directory:

				
					cd myproject
				
			

Step 3: Adding an App

In Django, functionality is modularized into apps. Let’s create one:

				
					python manage.py startapp hello
				
			

This creates a directory structure for the hello app:

hello/

|– __init__.py

|– admin.py

|– apps.py

|– migrations/

    |– __init__.py

|– models.py

|– tests.py

|– views.py

Think of the app as a building block within your project, housing specific functionality.

Step 4: Connecting the App

Activate the hello app by adding it to your project settings. Open myproject/settings.py and include it under INSTALLED_APPS:

				
					INSTALLED_APPS = [
    ...,
    'hello',
]

				
			

Step 5: Writing Your First View

Views are the heart of any Django application. Let’s create a simple view in hello/views.py:

				
					from django.http import HttpResponse
				
			
				
					def hello_world(request):
    return HttpResponse("Hello, World!")

				
			

This function processes a web request and responds with “Hello, World!”.

Step 6: Mapping URLs

To make your view accessible, you need to map it to a URL. Create a urls.py file in the hello directory:

For Windows users, use this command to create the file:

				
					echo. > hello/urls.py
				
			

Then, add the following code to hello/urls.py:

				
					from django.urls import path
from . import views

urlpatterns = [
    path('', views.hello_world, name='hello_world'),
]

				
			

Next, include the hello app’s URLs in the project’s main urls.py:

				
					from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('hello.urls')),
]

				
			

Step 7: Running the Server

Bring your project to life by starting Django’s development server:

				
					python manage.py runserver
				
			

Visit http://127.0.0.1:8000/ in your web browser, and you’ll see the magic words: “Hello, World!”.

Step 8: Reflect and Celebrate

You’ve just created your first Django project! This seemingly simple “Hello, World!” marks the beginning of your journey into web development with Django. From here, the possibilities are endless—dynamic websites, APIs, and beyond.

Beyond Basics

Once you’re comfortable with these steps, explore Django’s rich feature set:

  1. Models: Structure your data with Django’s ORM.
  2. Templates: Design dynamic HTML pages.
  3. Static Files: Manage CSS, JavaScript, and images.

For more, visit the official Django documentation.

Conclusion

Starting with Django might seem daunting at first, but as you’ve seen, creating your first project is straightforward. With Django, you’re not just building applications; you’re building a foundation for learning and growing as a web developer. The “Hello, World!” you created today is more than a simple message—it’s the first step toward mastering a framework that powers countless websites and applications worldwide.

Tech Amplifier Final Logo