Lesson 1
Setting Up a Basic Django Project
Setting Up a Basic Django Project

Welcome to your first step into back-end engineering with Django! This lesson is your starting point in building powerful web applications. By the end of this unit, you'll have a fully functional basic Django project up and running. This foundation will be essential for everything we do moving forward.

What You'll Learn

In this unit, we'll focus on setting up a basic Django project. This involves creating a Django project and app, configuring essential settings, defining a simple view, and mapping URLs. Here's a brief overview of what you'll be doing:

Creating a Django Project and App: To build a Django project, we need to create a project and an app. A project is the entire web application, while an app is a web application component that performs a specific function. We'll create a project called myproject and an app called myapp. In the snippet below, we create a project and an app using the Django CLI:

Shell Session
1mkdir project && cd project 2django-admin startproject myproject 3python manage.py startapp myapp

This will create a project structure like this:

1project/ 2 myproject/ 3 __init__.py 4 settings.py 5 urls.py 6 wsgi.py 7 myapp/ 8 __init__.py 9 admin.py 10 apps.py 11 models.py 12 tests.py 13 views.py 14 manage.py 15 db.sqlite3

Let's break down the purpose of each file:

  • myproject: The project directory that contains the settings, URLs, and WSGI configuration.
    • settings.py: Contains the project settings such as installed apps, middleware, and database configuration.
    • urls.py: Maps URLs to views in the project, so Django knows what to do when a request comes into a particular URL pattern.
    • wsgi.py: Contains the project's WSGI (Web Server Gateway Interface) configuration. This helps Django communicate with the web server, but we won't cover this file in this course path.
  • myapp: The app directory that contains the app-specific files.
    • views.py: The views (functions) that handle HTTP requests and return responses.
    • models.py: Contains the database models for the app. We will cover this in future courses.
    • admin.py: Contains the configuration for the Django admin interface. We won't cover this in this course path.
    • apps.py: Contains the app configuration for myapp.
    • tests.py: Contains the tests for the app. We won't cover this in this course path.
  • The manage.py file is a command-line utility that lets you interact with the Django project. You can use it to run the development server, create database migrations, and more.
  • db.sqlite3 is the default SQLite database that Django uses for development. We'll cover databases in future courses.

Configuring Settings: You will modify the settings.py file to include your new app so Django recognizes it:

Python
1INSTALLED_APPS = [ 2 'myapp', # This is the app we created 3 'csp', 4 'django.contrib.admin', 5 'django.contrib.auth', 6 'django.contrib.contenttypes', 7 'django.contrib.sessions', 8 'django.contrib.messages', 9 'django.contrib.staticfiles', 10]

Creating Views and Mapping URLs: You'll create a simple view to handle HTTP requests and then map it to a URL in your project.

We define the view in views.py with a simple function home that returns an HTTP response:

Python
1# project/myapp/views.py 2from django.http import HttpResponse 3 4def home(request): 5 return HttpResponse('Hello, world!')

Next, we map this view to a URL in the project's urls.py file using the path function:

Python
1# project/myproject/urls.py 2from django.contrib import admin 3from django.urls import path 4from myapp import views 5 6urlpatterns = [ 7 path('', views.home, name='home'), 8]

The path basically maps the root URL, which is an empty string '', to the home view we defined in myapp/views.py. This endpoint will return the text Hello, world! when you visit the root URL http://localhost:3000/.

Once the project and app are created, you can run the development server using the following command:

Shell Session
1python manage.py runserver

This will start the development server and you can query the app at http://127.0.0.1:3000/ in your browser or from a script and get the response from the server.

Note that the project will be fully configured, and the server will be up and running for you at http://127.0.0.1:3000/, so you will not need to do anything on this matter in the practice section.

Why It Matters

Mastering the setup of a Django project is crucial for any back-end developer. This initial step lays the groundwork for more advanced functionalities like handling data, serving static files, and user authentication. Understanding this process ensures you can confidently start new projects and create robust web applications that can scale and evolve.

Exciting, right? Let's get your hands dirty with the practice section and bring your first Django project to life!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.