장고

6626 단어
Django7learn at$ source venv/bin/activate
structure

블로그-urls.py

    path('list/', post_list),
    path('archive/<int:year>', post_list),
    path('archive/<int:year>/<int:month>', post_list),


blog-views.py
if 월이 if year 앞에 와야 하는 경우

def post_list(request, year=None, month=None):

    if month is not None:
        return HttpResponse(f"post list archive for {year} and {month}")
    if year is not None:
        return HttpResponse(f"post list archive for {year}")

    return HttpResponse("Posts list page")





블로그 앱에서 utils.py 생성

class FourDigitYear:
    regex = '[0-9]{4}'

    def to_python(self, value):
        return int(value)

    def to_url(self, value):
        return '%04d' % value



from django.urls import path, register_converter

from blog.utils import FourDigitYear
from blog.views import post_list, categories_list, post_detail

register_converter(FourDigitYear, 'fourdigit')

urlpatterns = [
    path('list/', post_list),
    path('archive/<int:year>', post_list),
    path('archive/<fourdigit:year>/<int:month>', post_list),
    path('detail/<slug:post_slug>/', post_detail),
    path('categories/list', categories_list),
]






또 다른 방법:
블로그-urls.py

from django.urls import path, re_path

re_path(r"archive/(?P<year>[0,9]{4})/", post_list),

좋은 웹페이지 즐겨찾기