ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • TIL 2023.06.15
    내일배움캠프 2023. 6. 15. 20:19

    어제부로 내 할일은 대충 끝내서 오늘은 다른 일을 했다.

     

    스팟 앱을 만들어서 Area, Sigungu, Spot 모델을 루트 앱에서 분리했다.

    관련해서 뷰도 만들고

    특히 spot뷰는 지역 필터링 기능과 제목으로 검색하는 기능 + 페이지네이션까지 구현했다.

     

    django-filter 라는 패키지로 필터링을 간단하게 구현할 수 있었고

    drf에 내장된 ListAPIView 으로 페이지네이션과 필터링 검색까지 간단하게 구현할 수 있다.

     

    from rest_framework.generics import ListAPIView
    from django_filters.rest_framework import DjangoFilterBackend
    
    from spots.models import Spot
    from spots.serializers import SpotSerializer
    
    
    class SpotFilterView(ListAPIView):
        queryset = Spot.objects.all().order_by("id")
        serializer_class = SpotSerializer
        filter_backends = [DjangoFilterBackend, SearchFilter]
        filterset_fields = ["type", "area", "sigungu",]
        search_fields = ['title',]

    코드는 이런식

     

    페이지네이션에 관한 건 settings.py에 디폴트로 지정해 줄 수 있다.

    REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
        'PAGE_SIZE': 6,
    }

     

    또한 루트앱에도 페이지네이션을 적용했다.

     

     

    조리로 페이지도 조금 업데이트 되었다.

        const model = document.getElementById("model-select").value;
        if (!model) {
            return alert("모델을 선택해 주세요.");
        };
        const place = document.getElementById("place-select").value;
        if (!place) {
            return alert("여행지를 선택해 주세요.");
        };
        const image = document.getElementById("image-select").files[0];
        if (!image) {
            return alert("내 사진을 선택해 주세요.");
        };

    이렇게 value가 없으면 경고를 날린다.

    '내일배움캠프' 카테고리의 다른 글

    WIL 내일배움캠프 14주차  (0) 2023.06.16
    TIL 2023.06.16  (0) 2023.06.16
    TIL 2023.06.14  (2) 2023.06.14
    TIL 2023.06.13  (0) 2023.06.13
    TIL 2023.06.12  (0) 2023.06.12
Designed by Tistory.