Creating a Basic Django App: Your First Project

Dec. 19, 2024



Django is a high-level Python web framework that allows developers to build robust and scalable web applications. Starting your first Django project might seem daunting, but this guide simplifies the process and helps you get up and running in no time.


What You’ll Learn

  1. Installing Django
  2. Setting up a Django project
  3. Creating a basic Django app
  4. Running your application locally

Step 1: Install Django

Before starting, ensure you have Python installed on your machine. Install Django using pip:

bash
pip install django

To verify the installation, run:

bash
django-admin --version

Step 2: Create a Django Project

A Django project serves as the configuration hub for your entire application. To create a project, use the following command:

bash
django-admin startproject myproject

This creates a folder structure as follows:

markdown
myproject/ ├── manage.py ├── myproject/ │ ├── __init__.py │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ ├── wsgi.py
  • manage.py: A command-line utility for managing the project.
  • settings.py: Configurations like database, installed apps, and middleware.
  • urls.py: URL routing for your application.

Navigate into the project folder:

bash
cd myproject

Step 3: Run the Development Server

Start the Django development server to see if everything is working:

bash
python manage.py runserver

Visit http://127.0.0.1:8000 in your browser. You should see the default Django welcome page.


Step 4: Create a Basic App

In Django, an app is a web component that performs a specific function. To create an app named blog, run:

bash
python manage.py startapp blog

This creates the following structure:

markdown
blog/ ├── admin.py ├── apps.py ├── migrations/ │ └── __init__.py ├── models.py ├── tests.py ├── views.py

Step 5: Register the App

To include your app in the project, register it in INSTALLED_APPS in settings.py:

python
INSTALLED_APPS = [ # Other apps 'blog', ]

Step 6: Create a Basic View

Open blog/views.py and define your first view:

python
from django.http import HttpResponse def home(request): return HttpResponse("Welcome to my first Django app!")

Step 7: Add a URL for the View

To make the view accessible, add a URL mapping. Create a new file blog/urls.py and add:

python
from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ]

Now, connect this app's URLs to the main project. In myproject/urls.py, include the blog URLs:

python
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog.urls')), # Connect blog app ]

Step 8: Run the Server

Start the server again:

bash
python manage.py runserver

Visit http://127.0.0.1:8000 in your browser. You should see:
"Welcome to my first Django app!"


Step 9: Add a Template (Optional)

Instead of returning plain text, let’s use an HTML template. Create a templates folder inside blog and add a file named home.html:

arduino
blog/ ├── templates/ │ └── home.html

Content of home.html:

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My First Django App</title> </head> <body> <h1>Welcome to my first Django app!</h1> </body> </html>

Update the home view in views.py to render the template:

python
from django.shortcuts import render def home(request): return render(request, 'home.html')

Refresh your browser to see the styled HTML page.


Step 10: Next Steps

Congratulations! You've created your first Django project and app. Here’s what you can explore next:

  1. Models: Add database-backed functionality.
  2. Admin Panel: Use the built-in admin interface.
  3. Forms: Create dynamic forms for user input.
  4. Static Files: Add CSS, JavaScript, and images.

Conclusion

This guide introduced the basics of creating a Django project and app. With Django’s robust framework, you can now start building dynamic and feature-rich web applications. Keep experimenting and exploring Django’s features to enhance your development skills!