Creating Your First Django App

Tutorial 3 of 5

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.