A Comprehensive Guide to Implementing and Installing a Django App

Fahiz
3 min readAug 30, 2024

--

Introduction

Django is a powerful and versatile web framework allowing developers to quickly build robust web applications. In this guide, we’ll walk through the process of setting up a Django app from scratch, covering everything from installation to implementation. Whether you’re a beginner or looking to brush up on your Django skills, this tutorial has you covered.

1. Setting Up Your Environment

Before diving into Django, ensuring your environment is properly configured is essential. Here’s what you’ll need:

1.1. Install Python

Django is a Python-based framework, so you’ll need Python installed on your machine. You can download the latest version of Python from the official Python website.

After installation, verify the installation by running:

python --version

1.2. Install pip

pip is Python's package installer, which you’ll use to install Django. Most Python installations come with pip pre-installed. Check if it's installed by running:

pip --version

If pip isn’t installed, you can install it by following the instructions here.

2. Installing Django

With Python and pip set up, you can now install Django. To install the latest version of Django, run:

pip install django

To verify the installation, check the version of Django:

django-admin --version

3. Creating a New Django Project

Now that Django is installed, let’s create a new Django project.

3.1. Start a New Project

Use the django-admin command to create a new project:

django-admin startproject myproject

This command will create a new directory called myproject with the following structure:

myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py

3.2. Understand the Project Structure

  • manage.py: A command-line utility that lets you interact with the Django project.
  • settings.py: Contains all the configuration for your project.
  • urls.py: The URL declarations for your project.
  • wsgi.py: An entry point for WSGI-compatible web servers.

4. Running the Development Server

To see if everything is working correctly, navigate into your project directory and run the development server:

cd myproject
python manage.py runserver

Visit http://127.0.0.1:8000/ in your web browser. If you see the “Welcome to Django” page, congratulations—you’ve successfully set up your Django project!

5. Creating Your First App

In Django, an app is a web application that does something — like a blog, a database of public records, or a simple poll. To create an app, run the following command within your project directory:

python manage.py startapp myapp

This will create a new directory myapp/ with the following structure:

myapp/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py

6. Adding the App to Your Project

To integrate your new app with the project, you need to add it to the INSTALLED_APPS list in settings.py:

# myproject/settings.py

INSTALLED_APPS = [
...
'myapp',
]

7. Defining Models

Models are a way to create, retrieve, update, and delete records in your database. Let’s create a simple model in myapp/models.py:

# myapp/models.py

from django.db import models

class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()

8. Creating and Applying Migrations

After defining your models, create and apply migrations to update the database schema:

python manage.py makemigrations
python manage.py migrate

9. Creating Views and Templates

Views handle the logic for your app, while templates determine how the data is presented. Create a view in myapp/views.py:

# myapp/views.py

from django.shortcuts import render

def home(request):
return render(request, 'home.html')

Next, create a template file in a directory called templates within myapp/:

<!-- myapp/templates/home.html -->

<!DOCTYPE html>
<html>
<head>
<title>My Django App</title>
</head>
<body>
<h1>Welcome to My Django App</h1>
</body>
</html>

10. Mapping URLs

Finally, map a URL to the view in myapp/urls.py:

# myapp/urls.py

from django.urls import path
from . import views

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

Include this URL configuration in the main urls.py of your project:

# myproject/urls.py

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

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

11. Running the App

Now, start the server again:

python manage.py runserver

Navigate to http://127.0.0.1:8000/ in your browser, and you should see your custom homepage!

12. Conclusion

You’ve just created and run a basic Django app! From setting up your environment to creating views and templates, you’ve covered the essential steps in Django development. Django is a powerful tool with endless possibilities, and this guide is just the beginning. Keep exploring and building!

--

--

Fahiz
Fahiz

No responses yet