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
mkdir project && cd project
django-admin startproject myproject
python manage.py startapp myapp

This will create a project structure like this:

project/
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    myapp/
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        views.py
    manage.py
    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
INSTALLED_APPS = [
    'myapp', # This is the app we created
    'csp',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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
# project/myapp/views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse('Hello, world!')

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

Python
# project/myproject/urls.py
from django.contrib import admin
from django.urls import path
from myapp import views

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

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
python 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.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal