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.
Table of Contents
Step 1: Starting a New Django Project
- Install Django: If you haven’t already, install Django using pip by running the following command:
pip install Django
- 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”:
myproject/ ├── myproject/ │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ ├── asgi.py │ └── wsgi.py ├── myapp/ │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations/ │ │ └── ... │ ├── models.py │ ├── tests.py │ └── views.py └── manage.py
myproject/: This is the main project directory.
myproject/: This inner directory is the Python package for your project.
__init__.py: An empty file that tells Python this directory should be considered a Python package.
settings.py: Configuration settings for your project.
urls.py: URL routing for your project.
asgi.py: ASGI application entry point (for asynchronous servers).
wsgi.py: WSGI application entry point (for traditional web servers).
myapp/: This is an example app within your project.
__init__.py: Also an empty file indicating this is a Python package.
admin.py: Configuration for the Django admin interface.
apps.py: App configuration.
migrations/: Database migration files.
models.py: Defines your application’s data models.
tests.py: Unit tests for your app.
views.py: Defines view functions and logic for your app.
manage.py: A command-line utility for managing your project.
With the project structure in place, let’s explore the core components of Django.
- Navigate to the Project Directory: Move into your project directory:
Step 2: Creating a New App
- Create a New App: To create a new app within your project, run the following command (replace “myapp” with your desired app name):
python manage.py startapp myapp
- Add the App to Your Project: Open the
myproject/settings.pyfile and add your app to the
INSTALLED_APPSlist. Add your app name, e.g., ‘myapp’, to the list as follows:
INSTALLED_APPS = [ ... 'myapp', ]
Step 3: Creating a Welcome Page
- Create a Template: Inside your app’s directory (
myapp), create a folder named ‘
templates‘ if it doesn’t already exist. Inside the
'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> <html> <head> <title>Welcome to Django World</title> </head> <body> <h1>Welcome to the Django Web Framework World!</h1> </body> </html>
Step 4: Creating a View
- Create a View: In your app’s directory (e.g.,
myapp), open the
views.pyfile 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
- 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
- Include the App URLs in Project URLs: Open the ‘
'file and include your app’s URL configuration. Add the following line to include your app’s URLs:
Step 6: Configuring Settings
- Static Files Configuration: In your ‘
myproject/settings.py‘, 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
- Migrate the Database: Run the following commands in command prompt to create and apply migrations:
python manage.py makemigrations python manage.py migrate
This creates the necessary database tables.
Step 8: Starting the Development Server
- Start the Development Server: Run the following command to start the development server:
python manage.py 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
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!
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.