fbpx

16 – Steps to Build a Web App Using Python Django Framework – Full Guide

Python Django is a powerful web framework that simplifies and streamlines the process of building web applications. Known for its robustness, flexibility, and adherence to the “Don’t Repeat Yourself” (DRY) principle, Django has gained widespread popularity among developers.

One of Django’s key features is its emphasis on clean, reusable code. The framework follows the Model-View-Controller (MVC) architectural pattern, organizing code into models, views, and templates. Models define the data structure, views handle user interaction, and templates manage the presentation layer. This separation of concerns enhances code maintainability and encourages efficient collaboration among development teams.

Setting up a virtual environment for a Django project involves creating a virtual environment, activating it, installing Django, and then creating a new Django project within that environment. Here are the steps to set up a virtual environment and a Django project:

Step 1: Install Virtualenv (if not installed):

If you haven’t installed virtualenv globally, you can do so using the following command:

pip install virtualenv

Step 2: Create a Virtual Environment:

Navigate to the directory where you want to create your Django project, and create a virtual environment:

# On Windows
python -m venv venv

# On macOS/Linux
python3 -m venv venv

This will create a virtual environment named “venv” in your project directory.

Step 3: Activate the Virtual Environment:

Activate the virtual environment:

  • On Windows:
venv\Scripts\activate
  • On macOS/Linux:
source venv/bin/activate

After activation, your terminal prompt should change to indicate that you are now working within the virtual environment.

Step 4: Install Django:

While the virtual environment is active, install Django using pip:

pip install django

Step 5: Create a Django Project:

Now, you can create a new Django project. Replace “myproject” with your preferred project name:

django-admin startproject myproject

Step 6: Navigate to the Project Directory:

Move into the newly created project directory:

cd myproject

Step 7: Run Migrations:

Run initial database migrations:

python manage.py migrate

Step 8: Create a Superuser (Optional):

Optionally, you can create a superuser account for the Django admin:

python manage.py createsuperuser

Step 9: Run the Development Server:

Start the development server:

python manage.py runserver

Visit http://127.0.0.1:8000/ in your browser to see the Django welcome page.

That’s it! You’ve successfully set up a virtual environment and created a new Django project. Remember to activate the virtual environment every time you work on your project by running the activation command from Step 3.

let’s go through the steps to create a new app in a Django project, edit the settings.py file, and create a simple view. For this example, let’s assume you already have a Django project set up.

Step 10: Create a New App

In your Django project directory, run the following command to create a new app. Replace “myapp” with your preferred app name:

python manage.py startapp myapp

Step 11: Add the App to Installed Apps

Open your project’s settings.py file located in the project directory and add your new app to the INSTALLED_APPS list:

# myproject/settings.py

INSTALLED_APPS = [
    # ...
    'myapp',
    # ...
]

Step 12: Create a Simple View

Inside your new app directory (myapp in this example), open the views.py file and define a simple view:

# myapp/views.py

from django.http import HttpResponse

def my_view(request):
    return HttpResponse("Hello, this is my first view!")

Step 13: Create a URL Pattern

Create a urls.py file in your app directory and define a URL pattern for the view:

# myapp/urls.py

from django.urls import path
from .views import my_view

urlpatterns = [
    path('my-view/', my_view, name='my-view'),
]

Step 14: Include the App’s URLs in the Project

In your project’s urls.py file, include the URLs from your app:

# myproject/urls.py

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

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

Step 15: Run Migrations

If you’ve made any changes to your models (which we haven’t in this example), you should run migrations:

python manage.py makemigrations
python manage.py migrate

Step 16: Run the Development Server

Start the development server:

python manage.py runserver

Visit http://127.0.0.1:8000/myapp/my-view/ in your browser to see the output from your new view.

That’s it! You’ve created a new app, added it to the project, defined a simple view, and configured URL patterns to access the view. You can now expand on this foundation by adding more views, templates, and functionality to your Django app.

Scroll to Top