-
Notifications
You must be signed in to change notification settings - Fork 0
Add project views
At the end of this guide, you will:
- prepare the project structure for the future development
- create base project views
git checkout bp-views
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.
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"
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",
]
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"
Within tasks/views.py module add views function as follows.
def task_list_view(request: HttpRequest) -> HttpResponse:
"""
Handle requests to tasks list
"""
return HttpResponse("tasks list")
def task_detail_view(request: HttpRequest, pk: int) -> HttpResponse:
"""
Handle requests to task details
"""
return HttpResponse("tasks detail: %d" % pk)
def task_create_view(request: HttpRequest) -> HttpResponse:
"""
Handle requests to create a new task instance
"""
return HttpResponse("tasks create")
def task_update_view(request: HttpRequest, pk: int) -> HttpResponse:
"""
Handle requests to update an existing task instance
"""
return HttpResponse("tasks update: %d" % pk)
def task_delete_view(request: HttpRequest, pk: int) -> HttpResponse:
"""
Handle requests to delete an existing task instance
"""
return HttpResponse("tasks delete: %d" % pk)
Within users/views.py module add views functions as follows.
def user_profile_view(request: HttpRequest) -> HttpResponse:
"""
Handle requests to user's profile
"""
return HttpResponse("user profile")
def sign_up_view(request: HttpRequest) -> HttpResponse:
"""
Register a new user in the system
"""
return HttpResponse("sign up")
def sign_in_view(request: HttpRequest) -> HttpResponse:
"""
Authenticate a user
"""
return HttpResponse("sign in")
def sign_out_view(request: HttpRequest) -> HttpResponse:
"""
Sing out the authenticated user
"""
return HttpResponse("sign out")
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),
]
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/ |
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")),
]
- Set up new Django project
- Add project views
- Configure PostgreSQL database
- Add task model
- Fetch data from the database to response
- Render HTML templates to response
- Create auth forms
- Implement user authentication
- Create custom user model
- Create task model form
- Apply permissions to views
- Refactoring project views to CBVs
- Create serializers
- Implement API views
- Apply API views permissions