Programming-[Backend] (394) 썸네일형 리스트형 Django로 Pinterest 따라 만들기-12. ListView/Pagination, Mixin, CommentApp 구현 1. ListView/Pagination 이제 기존에 Lorem Picsum으로 간단히 적용해놓았던 List 페이지를 실제로 사용자가 입력하는 Article들의 리스트가 조회되도록 수정한다. ListView 적용 임시로 적용해두었던 TemplateView를 ListView로 변경한다. ListView도 CRUD 관련 View들과 마찬가지로 django에서 기본적으로 제공해주는 view이며, 여기에는 pagination option도 지정할 수 있다. articleapp/views.py ArticleListView를 추가한다. ListView를 상속받고, paginate_by 옵션을 설정해준다. 5로 두어 한 페이지당 5개만 출력되도록 해본다. class ArticleListView(ListView): m.. Django로 Pinterest 따라 만들기-11. MagicGrid 활용, ArticleApp CRUD 완성하기 1. Articleapp.list 생성 / MagicGrid 적용 Articleapp을 만들고 나서 Article list가 반응형 사진 앨범처럼 나오게 해주는 MagicGrid를 적용해볼 것이다. Articleapp 기본 틀 생성 -articleapp을 시작한다. 'python manage.py startapp articleapp' -pragmatic/settings.py에 추가 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'a.. Django로 Pinterest 따라 만들기-10. Profileapp 마무리, 리팩토링 1. ProfileUpdateView ProfileUpdateView 생성 createView와 거의 동일하게 생성한다. views.py 일부 class ProfileUpdateView(UpdateView): model = Profile context_object_name = 'target_profile' form_class = ProfileCreationForm success_url = reverse_lazy('accountapp:hello_world') template_name = 'profileapp/update.html' update.html enctype을 추가해주었다. {% extends 'base.html' %} {% load bootstrap4 %} {% block content %} Up.. Django로 Pinterest 따라 만들기-9. ModelForm 사용, Profileapp 구현 이제 accountapp 외에 Profileapp을 만들어본다. 1. pillow 라이브러리 profileapp 작성 전, pillow 라는 라이브러리를 설치한다. 'pip install pillow' 파이썬의 pillow 라이브러리는 이미지 처리 라이브러리다. 이미지 포맷 변환, 이미지 파일 읽고 쓰기, 이미지 편집 등의 기능을 제공한다. 나중에 이미지 처리가 필요한 부분에서 이 라이브러리를 사용할 것이다. 2. ProfileApp 시작 profileApp 생성 accountapp을 만들었을 때와 마찬가지로 profileapp을 만든다. 1. profileapp 생성 : 'python manage.py startapp profileapp' 명령어 입력 2. INSTALLED_APPS 등록 : pragm.. Django로 Pinterest 따라 만들기-8. Authentication, Decorator 활용, superuser 및 media 설정 1. Authentication 기능 구현 메인 페이지 접근 제한 로그인한 사용자만 메인 페이지에 접근할 수 있도록 아래 코드와 같이 접근 제한 코드를 추가한다. 특별할게 없고, 기존 코드를 if, else 문으로 감싸서 처리한다. django에서 제공해주는 requet.user.is_authenticated로 로그인 여부를 검사할 수 있다. 로그인이 되지 않았다면 HttpResponseRedirect를 이용해서 로그인 페이지로 보내도록 한다. views.py def hello_world(request): if request.user.is_authenticated: if request.method == 'POST': #기존 코드 생략 else: return HttpResponseRedirect(rever.. Django로 Pinterest 따라 만들기-7. Update, DeleteView 구현 1. UpdateView UpdateView 작성 updateView는 createView와 거의 동일하다. views.py 일부 form_class는 UserCreationView를 그대로 이요하였다. class AccountUpdateView(UpdateView): model = User context_object_name = 'target_user' form_class = UserCreationForm success_url = reverse_lazy('accountapp:hello_world') template_name = 'accountapp/update.html' update.html {% url %} 내부에 pk=user.pk로 로그인한 사용자 본인의 정보를 수정할 수 있도록 하였다. {% e.. Django로 Pinterest 따라 만들기-6. BootStrap 추가, DetailView 구현 1. BootStrap 추가 디자인을 수정하기 위해서 BootStrap을 추가한다. django에는 Django-Bootstrap4 라는 전용 라이브러리가 있다. 아래 링크에서 확인할 수 있다. https://django-bootstrap4.readthedocs.io/en/latest/index.html 우선 설치를 위해서 Installation 페이지에 적혀있는대로 아래 명령어를 터미널에 입력한다. pip install django-bootstrap4 그리고 설치가 됬다는 것을 알려주기 위해 settings.py의 INSTALLED_APPS에 bootstrap4를 추가한다. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'djang.. Django로 Pinterest 따라 만들기-5. AccountApp : CBV, CreateView, Login, reverse_lazy 이제 Pinterest 서비스를 구현하기 위한 Account, Authentication, Profile, Article, Comment 등의 app 객체와 서비스를 만든다. 가장 기초가 되는 회원 정보인 AccountApp에서부터 시작한다. 1. 함수형 View(FBV)와 클래스형 View(CBV)의 차이 기존 HelloWorld 객체를 관리하는 View는 아래와 같이 함수로 정의하고, if 분기문을 통해서 CRUD, HTTP Method를 구분했었다. def hello_world(request): if request.method == 'POST': # 처리 로직 생략 return HttpResponseRedirect(reverse('accountapp:hello_world')) else: return.. 이전 1 ··· 19 20 21 22 23 24 25 ··· 50 다음