Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Add project views

Serhii Horodilov edited this page Feb 20, 2024 · 2 revisions

At the end of this guide, you will:

  • prepare the project structure for the future development
  • create base project views

Table of Contents

Getting started

git checkout bp-views

Guide

Create Django applications

Create new applications named tasks and users.

python manage.py startapp tasks
python manage.py startapp users

This will create two applications within the project root.

Understanding Django apps

In Django, applications are modular components that encapsulate specific functionality. They contain models, views, templates, and other resources related to a particular feature or aspect of the project. Applications promote code organization and maintainability.

Each application has its config, which is the class inherited from django.apps.AppConfig.

# tasks/apps.py
from django.apps import AppConfig


class TasksAppConfig(AppConfig):
    name = "tasks"
# users/apps.py
from django.apps import AppConfig


class UsersAppConfig(AppConfig):
    name = "users"

Install apps

To install apps to the Django project you have to add paths to their configuration classes to INSTALLED_APPS list in tasktracker/settings.py. You may use short form ("tasks") or full path ("tasks.apps.TasksAppConfig"). Note that paths are added as strings objects.

# tasktracker/settings.py
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",

    "tasks.apps.TasksAppConfig",
    "users.apps.UsersAppConfig",
]

Understanding the URL dispatcher

In Django, the URL dispatcher is responsible for directing incoming HTTP requests to the appropriate view based on the request's URL. The URL dispatcher uses a mapping of URL patterns to view functions, allowing for a clean and organized structure (see also ).

To design URLs for an app/project, you create a Python module called a URLConf (URL configuration). This module is pure Python code and is a mapping between URL path expressions to Python functions.

The root URLConf is defined in tasktracker/settings.py module.

ROOT_URLCONF = "tasktracker.urls"

Create tasks application views

Within tasks/views.py module add views function as follows.

List view

def task_list_view(request: HttpRequest) -> HttpResponse:
    """
    Handle requests to tasks list

    """

    return HttpResponse("tasks list")

Detail view

def task_detail_view(request: HttpRequest, pk: int) -> HttpResponse:
    """
    Handle requests to task details

    """

    return HttpResponse("tasks detail: %d" % pk)

Create view

def task_create_view(request: HttpRequest) -> HttpResponse:
    """
    Handle requests to create a new task instance

    """

    return HttpResponse("tasks create")

Update view

def task_update_view(request: HttpRequest, pk: int) -> HttpResponse:
    """
    Handle requests to update an existing task instance

    """

    return HttpResponse("tasks update: %d" % pk)

Delete view

def task_delete_view(request: HttpRequest, pk: int) -> HttpResponse:
    """
    Handle requests to delete an existing task instance
    
    """

    return HttpResponse("tasks delete: %d" % pk)

Create users application views

Within users/views.py module add views functions as follows.

User profile view

def user_profile_view(request: HttpRequest) -> HttpResponse:
    """
    Handle requests to user's profile

    """

    return HttpResponse("user profile")

Sign up view

def sign_up_view(request: HttpRequest) -> HttpResponse:
    """
    Register a new user in the system

    """

    return HttpResponse("sign up")

Sign in view

def sign_in_view(request: HttpRequest) -> HttpResponse:
    """
    Authenticate a user

    """

    return HttpResponse("sign in")

Sign out view

def sign_out_view(request: HttpRequest) -> HttpResponse:
    """
    Sing out the authenticated user

    """

    return HttpResponse("sign out")

Mapping views to URLs paths

Update root URLConf (tasktracker/urls.py), to include new views.

from django.contrib import admin
from django.urls import path

from tasks import views as tasks
from users import views as users

urlpatterns = [
    path("admin/", admin.site.urls),
    path("profile/", users.user_profile_view),
    path("sign-up/", users.sign_up_view),
    path("sign-in/", users.sign_in_view),
    path("sign-out/", users.sign_out_view),
    path("create/", tasks.task_create_view),
    path("<int:pk>/", tasks.task_detail_view),
    path("<int:pk>/update/", tasks.task_update_view),
    path("<int:pk>/delete/", tasks.task_delete_view),
    path("", tasks.task_list_view),
]

Run server and test new routes

List of implemented URL paths
Description URL
tasks list view http://localhost:8000/
task detail view
uses int argument
http://localhost:8000/42/
task create view http://localhost:8000/create/
task update view http://localhost:8000/42/update/
task delete view http://localhost:8000/42/delete/
user profile view http://localhost:8000/profile/
sign up view http://localhost:8000/sign-up/
sign in view http://localhost:8000/sign-in/
sign out view http://localhost:8000/sign-out/

Create app-level URLConf

Within tasks and users directories create Python modules named urls.py. Move app related URL patterns to corresponding modules.

# tasks/urls.py
from django.urls import path

from tasks import views

app_name = "tasks"
urlpatterns = [
    path("create/", views.task_create_view, name="create"),
    path("<int:pk>/", views.task_detail_view, name="detail"),
    path("<int:pk>/update/", views.task_update_view, name="update"),
    path("<int:pk>/delete/", views.task_delete_view, name="delete"),
    path("", views.task_list_view, name="list"),
]
# users/urls.py
from django.urls import path

from users import views

app_name = "users"
urlpatterns = [
    path("sign-up/", views.sign_up_view, name="sign-up"),
    path("sign-in/", views.sign_in_view, name="sign-in"),
    path("sign-out/", views.sign_out_view, name="sign-out"),
    path("profile/", views.user_profile_view, name="profile"),
]

app_name is required to use namespaces at inclusions. And include URL configurations to the root URLConf module.

# tasktracker/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("users.urls", namespace="users")),
    path("", include("tasks.urls", namespace="tasks")),
]

Changes

  1. Full change log
  2. Views and tests