본문 바로가기

프레임워크/Django

장고 (Django)

Start a project

django-admin startproject project_name # say wqtl

Start an app

# in the project folder where manage.py is located
python manage.py startapp app_name # say ohradical

URL dispatcher

Upon receiving a http request from a client, Django responds according to the procedure below:

  1. check ROOT_URLCONF in settings.py, the value of which becomes the starting point for mapping the given URL to the corresponding view. If a HttpRequest object contains a urlconf attribute, it overrides ROOT_URLCONF.

  2. Django loads the corresponding urls.py and looks for urlpatterns. If found, it starts searching for matching URL patterns and calls the corresponding view function (or class-based view).

urls.py

By default, there is urls.py in the root folder which ROOT_URLCONF points to.
urlpatterns contains a sequence of django.urls.path where each path contains a unique URL in a route argument.

urls.py can be created for each app and URL mapping between different urls.py can be connected via django.urls.include.

For example, create urls.py in /ohradical by which there are two urls.py:

.
├── ohradical
│   ├── migrations
│   ├── __pycache__
│   └── urls.py
└── wqtl
    ├── __pycache__
    └── urls.py

ROOT_URLCONF points to wqtl/urls.py which contains below:

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

urlpatterns = [
    path('ohradical/', include('ohradical.urls')),
    path('admin/', admin.site.urls),
]

localhost:8000/admin matches with the second path in urlpatterns where the first argument is admin/. Therefore, admin.site.urls is called to return the response.

In contrast, localhost:8000/ohradical, the first in urlpatterns, maps to include function that loads to /ohradicals/urls.py which contains below:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Note include takes the argument as string.
In the requested URL, localhost:8000/ohradical, there is no string after /ohradical. Therefore, the empty string '' is the matching pattern which then calls views.index, i.e., index function in ohradical/. More detail about path() is provided below.

django.urls.path

In urls.py, urlpatterns is an array containing a sequence of path() instances, each of which is responsible for mapping the requested URL to the corresponding view and takes the following arguments:

path(route, view, kwargs=None, name=None)

route (string | gettext_lazy()): route is a string that contains a URL pattern.

view (fn() from class views| as_views() | django.urls.include()): When Django finds a matching pattern, it calls the specified view function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments.

kwargs: Arbitrary keyword arguments can be passed in a dictionary to the target view.

name: Naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates. This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file.

Create an admin user

python manage.py createsuperuser

After creating a superuser account, log in on localhost:8000/admin
In the admin page, users and groups as well as databases of the apps can be managed.
If no apps appear but users and groups which are the default setting, register the app as below:

# project/app/admin.py
from django.contrib import admin
from .models import Question

admin.site.register(Question)