Django의 장점 중 하나는 admin 페이지가 자동으로 생성된다는 점입니다. 또한 admin 페이지를 통해서 입력이 잘 저장되는지 확인할 수 있습니다. 원래는 따로 테스트를 위해 form을 만드는데, Django의 admin 페이지를 통해 쉽게 테스트를 할 수 있게 되는 것입니다.
1. admin.py에 모델 등록
구름 IDE에서
accounts
폴더를 열면 admin.py
가 보입니다. 더블 클릭해서 에디터로 열겠습니다.

먼저, Profile model을 가져오겠습니다.
파일명 :
accounts/admin.py
from .models import Profile
이번에는 데코레이터를 이용해서 클래스를 만들겠습니다.
파일명 :
accounts/admin.py
from .models import Profile @admin.register(Profile)
모델을 불러옵니다.
파일명 :
accounts/admin.py
from .models import Profile @admin.register(Profile) class ProfileAdmin(admin.ModelAdmin):
admin 관련해서 미리 정해진 옵션들이 저장된 것을 사용합니다. admin 액션이라고 해서 Django 문서에 잘 정리 되어있는 내용입니다.
여기서 어떤 리스트를 보여줄지 적습니다.
파일명 :
accounts/admin.py
from .models import Profile @admin.register(Profile) class ProfileAdmin(admin.ModelAdmin): list_display = ['id', 'nickname', 'user']
id는 저희가 만들지 않았지만 Django에서 자동으로 생성해줍니다. list_display_links도 추가로 작성합니다.
파일명 :
accounts/admin.py
from .models import Profile @admin.register(Profile) class ProfileAdmin(admin.ModelAdmin): list_display = ['id', 'nickname', 'user'] list_display_links = ['nickname', 'user']
닉네임을 통해 유저를 검색할 수 있는 검색 창을 만들 수 있습니다.
파일명 :
accounts/admin.py
from .models import Profile @admin.register(Profile) class ProfileAdmin(admin.ModelAdmin): list_display = ['id', 'nickname', 'user'] list_display_links = ['nickname', 'user'] search_fields = ['nickname']
2. admin 페이지 접속
여기까지 작성한 것이 실제 admin 페이지에서 어떻게 구현이 되는지 확인하겠습니다.
admin 페이지에 접속하려면 웹 브라우저로 Django 서버에 접속할 때는 기본적으로 URL을 통해 접속해야 합니다.
urls.py
에서 admin 페이지 URL이 설정되어 있는지 확인해봅시다.config
폴더 아래 있는 urls.py
를 열어봅시다. 

urlpatterns 부분을 보시면 admin 페이지 URL이 이미 설정되어 있는 것을 볼 수 있습니다.

서버를 실행시켜서 admin 페이지에 접속해보겠습니다. 현재 가상환경이 활성화 되어있는지, 현재 디렉토리가 올바른지 확인 후 서버를 실행시켜봅니다.
(venv)root@goorm:/workspace/instaclone/instaclone# python manage.py runserver 0:80

프로젝트에서 실행 URL과 포트를 클릭합니다.

실행 URL을 클릭합니다.

이렇게 페이지가 보이게 됩니다.

이제 admin 페이지에 접속합니다. 아까
urls.py
를 확인했었습니다. 현재 URL 뒤에다가 /admin 을 추가합니다.https://*.run.goorm.io/admin
로그인 하는 페이지가 나오게 됩니다.

3. admin 유저 생성
admin 유저를 하나 생성할 필요가 생겼습니다. 슈퍼 유저를 만들도록 하겠습니다. 구름 IDE 터미널로 돌아가겠습니다.

서버가 작동 중 이기 때문에 CTRL + C 를 눌러서 서버를 종료시킵니다.

슈퍼 유저를 만들겠습니다.
(venv)root@goorm:/workspace/instaclone/instaclone# python manage.py createsuperuser
생성할 유저의 이름을 입력하라고 나옵니다. 자신이 원하는 것으로 입력하시면 됩니다.

유저 이름, 이메일, 비밀번호를 차례대로 입력해주시면 완료됩니다. 이메일은 선택사항이므로 반드시 입력할 필요는 없습니다. 비밀번호는 입력 시에 보이지 않으므로 참고해 주시길 바랍니다.

다시 서버를 실행시키고 admin 페이지로 들어가서 아까 생성한 유저로 로그인합니다. 로그인하면 이와 같은 화면이 나옵니다.

4. admin 페이지에서 객체 생성하기
유저와 그룹을 추가하고 변경할 수 있고
accounts
앱의 Profiles를 추가 및 수정할 수 있는 관리 페이지가 생겼습니다. 저희가 따로 만들지 않았지만 Django에서 자동으로 생성해 준 페이지입니다.우리는 클래스 이름을 Profile이라고 정의했지만 admin 페이지에서는 자동으로 s를 붙여서 복수형으로 보입니다.
Profile이 저희가 의도한 대로 만들어졌는지 확인하겠습니다.

Profiles 추가 버튼을 눌러봅니다. 이와 같은 화면이 출력됩니다.

유저, 별명, 사진, 자기 소개, 성별 우리가 만든 모델과 같이 구현이 되었습니다.
User를 추가해보도록 하겠습니다. User는 유저 목록을 불러온 것이기 때문에, 방금 생성한 슈퍼 유저만 있습니다.

별명은 "모기"로 입력하겠습니다.

프로필 사진은 아무 사진이나 고르면 됩니다.

소개는 "모기입니다."를 적었습니다.

성별은 남성으로 고르도록 하겠습니다.

그리고 저장 버튼을 누르겠습니다.

이와 같은 화면이 나옵니다. ID의 값은 Django가 자동으로 붙여주는 고유 숫자로 생성 순서마다 값이 다를 수 있습니다.

아까
admin.py
에서 설정한 대로 프로필 내용이 보이게 됩니다. id, nickname, user가 보이도록 list_display를 정하였고, list_display_links를 통해서 nickname과 user에 Profile 링크가 연결되도록 했습니다.
nickname으로 Profile을 검색할 수 있도록 했는데 지금 Profile이 1개여서 검색하는 의미가 없습니다. Profile을 하나 추가해봅시다.
먼저, 유저를 하나 생성하겠습니다. admin 페이지에서 사용자 추가 버튼을 누릅니다.

필요한 부분을 입력해서 사용자를 만들고 저장을 통해 완료합니다.

사용자를 만드셨으면 Profile을 하나 더 추가해봅시다. 추가하면 이렇게 2개의 프로필이 보입니다.

검색을 활용해 보면 별명에 해당하는 Profile이 보입니다.

별명의 일부만 입력해도 검색이 잘 됩니다.

nickname을 통해 검색하도록 설정했기 때문에 user 이름으로는 검색하지 못합니다.

첫 프로필을 만들 때, 프로필 사진을 업로드 했었습니다. 프로필 사진이 저장된 위치가 설정한 대로 잘 저장되었는지 확인해봅시다. 저장 위치를 설정한 곳은
settings.py
입니다. 미디어 파일 부분에서 MEDIA_URL을 보면 'media' 라는 폴더로 파일이 저장되는 것을 알 수 있습니다.
media
폴더를 열어보시면 accounts
폴더와 생성한 유저 이름과 jpg 파일이 보입니다.
이는 Profile 모델을 생성할 때 작성했던
models.py
의 user_path라는 함수와 관련있습니다.
accounts
라는 폴더 아래에 랜덤 알파벳 8개를 이름으로 하여 저장되게 했었습니다. 설정한 것과 같이 잘 된 것을 알 수 있습니다.또
models.py
의 Profile 모델에서 사진을 저장할 때, (150, 150)으로 Resize 하도록 했었습니다.
한 번 사진을 열어봅시다.

이와 같이 150X150 으로 크기가 조정된 채로 저장된 것을 볼 수 있습니다.

admin 페이지를 통해서 Profile을 생성하고 관리하며 프로필 사진까지 업로드 되는 것까지 확인했습니다.