Getting Started with Django

Tutorial 1 of 5

Introduction

In this tutorial, we will learn how to get started with Django, a high-level Python web framework. Our goal is to set up a Django project and create a simple web application.

By the end of this tutorial, you should be able to:

  • Install Django.
  • Start a new Django project.
  • Create and view a basic web page.

Prerequisites:

  • Basic knowledge of Python
  • Python installed on your machine

Step-by-Step Guide

  1. Installation: You can install Django via pip, Python’s package installer. Open your terminal and type:
pip install Django
  1. Starting a new project: After installation, we can create a new Django project using the command below:
django-admin startproject myproject

This will create a new Django project named "myproject."

  1. Starting the server: Navigate into your new project directory and start the server:
cd myproject
python manage.py runserver

You should see a message saying that the server is running and you can view the website at http://localhost:8000.

  1. Creating a new app: In Django, an app is a module within a project. Let's create an app named 'myapp':
python manage.py startapp myapp
  1. Creating a view: A view in Django is a Python function that receives a web request and returns a web response. In myapp/views.py, add this:
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django!")

This simple view returns "Hello, Django!" when it gets a web request.

Code Examples

Let's look at some code examples:

  1. Mapping the view to a URL: Django uses URLconfs to decide which code to run for each URL. In myapp/urls.py, add this:
from django.urls import path
from . import views

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

This code maps the URL of the homepage ('') to the home view.

  1. Including the app's URLconf in the project's URLconf: In myproject/urls.py, add this:
from django.urls import include, path

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

This code includes the URLs of 'myapp' in the project.

Summary

In this tutorial, we've learned how to install Django, start a new project, start the server, create an app, and create a view. The next step is to learn about Django's database models.

Resources:

Practice Exercises

  1. Exercise 1: Create a new Django project named 'exercise1' and start the server.
  2. Solution: Follow the steps in the 'Starting a new project' and 'Starting the server' sections.

  3. Exercise 2: In the 'exercise1' project, create a new app named 'myexerciseapp' and create a view that returns "Hello, Exercise!".

  4. Solution: Follow the steps in the 'Creating a new app' and 'Creating a view' sections, but return "Hello, Exercise!" instead of "Hello, Django!".

  5. Exercise 3: Map the view from Exercise 2 to the URL of the homepage.

  6. Solution: Follow the steps in the 'Mapping the view to a URL' and 'Including the app's URLconf in the project's URLconf' sections.

Tips for further practice:

  • Try changing the message in the view.
  • Try mapping the view to a different URL.