Django / Django Basics

Getting Started with Django

This tutorial will guide you through the basics of setting up a Django project and creating a simple web application. You'll learn how to install Django, start a new project, and …

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers the fundamental concepts of Django, including project structure, models, views, and templates.

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.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Scientific Calculator

Perform advanced math operations.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help