App 구현 - urls.py (URL Patterns)

Hansu Kim·2021년 9월 15일
0

Django

목록 보기
4/10

Django는 URLconf를 통해 URL패턴을 뷰로 연결한다.

Procedure of processing a HTTP request in Django

  1. HTTP Request를 django 서버에서 수신
  2. Django에서는 기본적으로 ROOT_URLCONF 값에 따라 어떤 root URLconf module을 사용할지 결정한다.
    하지만 만약 해당 Request가 미들웨어에 의해 정의된 urlconf attribute를 가지고 있을 경우에는 ROOT_URLCONF 대신 자체 값에 따라 처리한다.
  3. Django는 파이썬 모듈을 로드하고, urlpatterns 매칭값을 찾는다.
    • 해당 sequence는 django.urls.path() 혹은 django.urls.re_path() 인스턴스에 정의되어있어야 함.
  4. URL 패턴이 매칭되면, Django는 해당 패턴에 주어진 Python Function(Class-based-View)를 import/call 한다.
    • 만약 매칭된 패턴이 다른 별도 그룹을 포함하고 있지 않다면, 정규식의 일치 항목이 positional argument로 제공됨(???)
      If the matched URL pattern contained no named groups, then the matches from the regular expression are provided as positional arguments.
    • 매칭 실패시, 그에 맞는 적절한 에러 핸들링 뷰를 호출함.

Path converters

The following path converters are available by default:

  • str - Matches any non-empty string, excluding the path separator, '/'. This is the default if a converter isn’t included in the expression.
  • int - Matches zero or any positive integer. Returns an int.
  • slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
  • uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
  • path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than a segment of a URL path as with str.

출처: https://docs.djangoproject.com/ko/3.2/topics/http/urls/

0개의 댓글