Django / Django Basics

Creating Your First Django App

This tutorial is designed to help beginners create their first web application using Django. We'll walk through the process of creating a new app, defining a model, creating a vie…

Tutorial 3 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 aim to guide you through the process of creating your first Django web application. We'll create a new app, define a model, create a view, and set up a URL for your app.

By the end of this tutorial, you will be able to:
- Set up a new Django project and app
- Create a basic model in Django
- Understand Django's MVT (Model, View, Template) architecture
- Create a simple view and map it to a URL

Prerequisites:
- Basic knowledge of Python
- Django installed on your system

Step-by-Step Guide

Creating a new Django project

  1. Open your terminal and navigate to the directory where you want your project to live.
  2. Type django-admin startproject myproject to create a new Django project. Replace 'myproject' with the name you want for your project.

Creating a new Django app

  1. In your terminal, navigate to your project directory using cd myproject.
  2. Type python manage.py startapp myapp to create a new app within your project. Replace 'myapp' with the name you want for your app.

Defining a model

In Django, a model is a representation of a database table. Let's create a simple model:

  1. Navigate to models.py inside your app directory.
  2. Define a new model as follows:
from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

Creating a view

A view in Django is a Python function that receives a web request and returns a web response. Let's create a simple view:

  1. Navigate to views.py inside your app directory.
  2. Define a new view as follows:
from django.http import HttpResponse
from .models import MyModel

def my_view(request):
    my_model = MyModel.objects.first()
    return HttpResponse(my_model.name)

Setting up a URL

  1. Create a new file called urls.py inside your app directory and define your URLs as follows:
from django.urls import path
from .views import my_view

urlpatterns = [
    path('', my_view, name='my_view'),
]
  1. In your project's urls.py, include the URLs of your app:
from django.urls import include, path

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

Code Examples

Here are the code snippets for the step-by-step guide, explained in detail:

models.py

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=100) # A character field with a maximum length of 100 characters
    description = models.TextField() # A text field for longer inputs

views.py

from django.http import HttpResponse
from .models import MyModel

def my_view(request):
    my_model = MyModel.objects.first() # Fetches the first entry in MyModel
    return HttpResponse(my_model.name) # Returns the name attribute of the fetched model

urls.py (App-level)

from django.urls import path
from .views import my_view

urlpatterns = [
    path('', my_view, name='my_view'), # Mapping the view to the root URL of the app
]

urls.py (Project-level)

from django.urls import include, path

urlpatterns = [
    path('myapp/', include('myapp.urls')), # Including all URLs from the 'myapp' app
]

Summary

In this tutorial, we learned how to set up a new Django project and app, define a basic model, create a simple view, and map that view to a URL. You can now create simple web applications using Django!

Next steps:
- Learn about Django's template system and create your first template
- Understand how to handle forms in Django
- Learn about Django's admin interface

Additional resources:
- Django's official documentation
- Django for Beginners

Practice Exercises

  1. Create a new Django project and app. Define a model with three different fields (CharField, TextField, and IntegerField).
  2. Create a view that fetches all entries in your model and returns their names in a HttpResponse.
  3. Map your view to a URL of your choice.

Solutions will vary depending on the specifics you chose for your project, app, model, and URL. As a tip for further practice, try to add more complex fields to your models, such as DateField or ForeignKey.

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

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Image Converter

Convert between different image formats.

Use tool

Watermark Generator

Add watermarks to images easily.

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