5-3. 사용자 어플리케이션 작성
startapp bookmark
이전 장에서 bookmark 어플리케이션을 설계할 때 5개의 화면 페이지가 필요했습니다. 이 5개의 페이지에 대해서 필요한 뷰 와 템플릿을 작성하겠습니다. 화면을 작성하기 위해서 처리흐름에 대한 로직이 설계되어야 합니다. 여기서는 4개의 URL와 뷰와 3개의 템플릿 파일이 필요합니다.
5-3-1. URL 설계
다음은 요청 URL 과 장고 View를 매핑한 리스트입니다. 각 뷰에서 처리하는 내용을 잘 이해해야 합니다.
URL 패턴 | 뷰 이름 | 뷰가 처리하는 내용 |
---|---|---|
/bookmark/ | list() | bookmark 리스트 템플릿을 보여 줍니다. |
/bookmark/add/ | add() | add.html 템플릿을 보여 줍니다. |
/bookmark/detail/1/ | detail() | detail.html 템플릿을 보여 줍니다. |
/bookmark/update/1/ | update() | update.html의 폼을 POST 방식으로 처리합니다. |
/bookmark/delete/1/ | delete() | delete.html 템플릿을 보여 줍니다. |
/admin/ | (기본제공) | 관리자 사이트를 보여 줍니다. (장고제공) |
5-3-2. 어플리케이션 생성
다음은 파이썬 startapp 명령어로 어플리케이션을 생성하도록 하겠습니다. bookmark라는 어플리케이션 명을 입력합니다.
windows command
C:\hyungsik74\pycharm\> cd bookmark-project
C:\hyungsik74\pycharm\bookmark-project> cd
C:\hyungsik74\pycharm\bookmark-project
C:\hyungsik74\pycharm\bookmark-project> python manage.py startapp bookmark
linux command
$/home/hyungsik74/pycharm> cd bookmark-project
$/home/hyungsik74/pycharm/bookmark-project> pwd
/home/hyungsik74/pycharm/bookmark-project
$/home/hyungsik74/pycharm/bookmark-project> python manage.py startapp bookmark
어플리케이션 생성후 디렉토리 구조입니다.
그림5-3-2. startapp 후 디렉토리
다음은 bookmark 어플리케이션에서 사용되는 전체 폴더 구조입니다. 참고하세요.
hyungsik74
└─ pycharm
└─ bookmark-project
├─ bookmark
| ├─ migrations
│ | ├─ __init__.py
│ | └─ 0001_initial.py
| ├─ templates
│ | ├─ bookmark
| │ | ├─ bookmark_confirm_delete.html
| │ | ├─ bookmark_create.html
| │ | ├─ bookmark_detail.html
| │ | ├─ bookmark_list.html
| │ | └─ bookmark_update.html
| ├─ __init__.py
| ├─ admin.py
| ├─ apps.py
| ├─ models.py
| ├─ tests.py
| ├─ urls.py
| └─ views.py
├─ config
| ├─ __init__.py
| ├─ asgi.py
| ├─ settings.py
| ├─ urls.py
| └─ wsgi.py
├─ static
| └─ style.css
├─ static_files
| └─ admin
| ├─ css
| ├─ fonts
| ├─ img
| └─ js
| └─ style.css
├─ templates
| └─ base.html
├─ db.sqlite3
└─ manage.py
hyungsik74
└─ pycharm
└─ bookmark-project
├─ bookmark
| ├─ migrations
│ | ├─ __init__.py
│ | └─ 0001_initial.py
| ├─ templates
│ | ├─ bookmark
| │ | ├─ bookmark_confirm_delete.html
| │ | ├─ bookmark_create.html
| │ | ├─ bookmark_detail.html
| │ | ├─ bookmark_list.html
| │ | └─ bookmark_update.html
| ├─ __init__.py
| ├─ admin.py
| ├─ apps.py
| ├─ models.py
| ├─ tests.py
| ├─ urls.py
| └─ views.py
├─ config
| ├─ __init__.py
| ├─ asgi.py
| ├─ settings.py
| ├─ urls.py
| └─ wsgi.py
├─ static
| └─ style.css
├─ static_files
| └─ admin
| ├─ css
| ├─ fonts
| ├─ img
| └─ js
| └─ style.css
├─ templates
| └─ base.html
├─ db.sqlite3
└─ manage.py
프로젝트 설정 파일(settings.py) 추가
# Application definition
INSTALLED_APPS = [
'bookmark', # 추가 bookmark.apps.bookmarkConfig 동일함.
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
# Application definition
INSTALLED_APPS = [
'bookmark', # 추가 bookmark.apps.bookmarkConfig 동일함.
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
5-3-3. 북마크 데이터모델 생성
데이터모델 생성은 장고 프레임웍에서 데이터베이스 관련 테이블 등을 생성을 하는 단계입니다. 다음의 순서대로 생성합니다.
windows command
C:\hyungsik74\pycharm\bookmark-project> cd bookmark
C:\hyungsik74\pycharm\bookmark-projectbookmark> cd
C:\hyungsik74\pycharm\bookmark-projectbookmark
// 테이블을 정의함
C:\hyungsik74\pycharm\bookmark-project\bookmark> notepad models.py
// 관리자화면에 표시
C:\hyungsik74\pycharm\bookmark-project\bookmark> notepad admin.py
linux command
$/home/hyungsik74/pycharm/bookmark-project> cd bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> pwd
/home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> vi models.py
$/home/hyungsik74/pycharm/bookmark-project/bookmark> vi admin.py
from django.db import models
from django.urls import reverse
# Create your models here.
# 모델 : 데이터베이스를 SQL없이 다루려고 모델을 사용
# 우리가 데이터를 객체화해서 다루겠다.
# 모델 = 테이블
# 모델의 필드 = 테이블의 컬럼
# 인스턴스 = 테이블의 레코드
# 필드의 값(인스턴스의 필드값) = 레코드의 컬럼 데이터값
class Bookmark(models.Model):
site_name = models.CharField(max_length=100)
url = models.URLField('Site URL')
# 필드의 종류가 결정하는 것
# 1. 데이터베이스의 컬럼 종류
# 2. 제약 사항 (몇글자까지)
# 3. Form의 종류
# 4. Form에서 제약 사항
def __str__(self):
return "이름 : "+self.site_name+", 주소 : "+self.url
def get_absolute_url(self):
return reverse('bookmark:detail', args=[self.id])
# 모델을 만들었다 => 데이터베이스에 어떤 데이터들을 어떤 형태로 넣을지 결정!
# makemigrations => 모델의 변경사항을 추적해서 기록
# 마이그레이션(migrate) => 데이터베이스에 모델의 내용을 반영(테이블 생성)
from django.db import models
from django.urls import reverse
# Create your models here.
# 모델 : 데이터베이스를 SQL없이 다루려고 모델을 사용
# 우리가 데이터를 객체화해서 다루겠다.
# 모델 = 테이블
# 모델의 필드 = 테이블의 컬럼
# 인스턴스 = 테이블의 레코드
# 필드의 값(인스턴스의 필드값) = 레코드의 컬럼 데이터값
class Bookmark(models.Model):
site_name = models.CharField(max_length=100)
url = models.URLField('Site URL')
# 필드의 종류가 결정하는 것
# 1. 데이터베이스의 컬럼 종류
# 2. 제약 사항 (몇글자까지)
# 3. Form의 종류
# 4. Form에서 제약 사항
def __str__(self):
return "이름 : "+self.site_name+", 주소 : "+self.url
def get_absolute_url(self):
return reverse('bookmark:detail', args=[self.id])
# 모델을 만들었다 => 데이터베이스에 어떤 데이터들을 어떤 형태로 넣을지 결정!
# makemigrations => 모델의 변경사항을 추적해서 기록
# 마이그레이션(migrate) => 데이터베이스에 모델의 내용을 반영(테이블 생성)
from django.contrib import admin
# Register your models here.
# 내가 만든 모델을 관리자 페이지에서 관리할 수 있도록 등록
from .models import Bookmark
admin.site.register(Bookmark)
from django.contrib import admin
# Register your models here.
# 내가 만든 모델을 관리자 페이지에서 관리할 수 있도록 등록
from .models import Bookmark
admin.site.register(Bookmark)
windows command
// DB 변경사항
C:\hyungsik74\pycharm\bookmark-project> py manage.py makemigrations bookmark
C:\hyungsik74\pycharm\bookmark-project> py manage.py sqlmigrate bookmark 0001
// DB 변경사항 반영
C:\hyungsik74\pycharm\bookmark-project> py manage.py migrate
// 실행
C:\hyungsik74\pycharm\bookmark-project> py manage.py runserver
linux command
$/home/hyungsik74/pycharm/bookmark-project> pwd
/home/hyungsik74/pycharm/bookmark-project
$/home/hyungsik74/pycharm/bookmark-project> python manage.py makemigrations bookmark
$/home/hyungsik74/pycharm/bookmark-project> python manage.py migrate
$/home/hyungsik74/pycharm/bookmark-project> python manage.py runserver
(venv) PS C:\hyungsik74\pycharm\bookmark-project> python manage.py startapp bookmark
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py makemigrations bookmark
Migrations for 'bookmark':
bookmark\migrations\0001_initial.py
- Create model Bookmark
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py sqlmigrate bookmark 0001
BEGIN;
--
-- Create model Bookmark
--
CREATE TABLE "bookmark_bookmark" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"site_name" varchar(100) NOT NULL,
"url" varchar(200) NOT NULL);
COMMIT;
(venv) PS C:\hyungsik74\pycharm\bookmark-project>
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, bookmark, contenttypes, sessions
Running migrations:
Applying bookmark.0001_initial... OK
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 24, 2023 - 12:55:42
Django version 4.2.5, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
(venv) PS C:\hyungsik74\pycharm\bookmark-project> python manage.py startapp bookmark
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py makemigrations bookmark
Migrations for 'bookmark':
bookmark\migrations\0001_initial.py
- Create model Bookmark
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py sqlmigrate bookmark 0001
BEGIN;
--
-- Create model Bookmark
--
CREATE TABLE "bookmark_bookmark" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"site_name" varchar(100) NOT NULL,
"url" varchar(200) NOT NULL);
COMMIT;
(venv) PS C:\hyungsik74\pycharm\bookmark-project>
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, bookmark, contenttypes, sessions
Running migrations:
Applying bookmark.0001_initial... OK
(venv) PS C:\hyungsik74\pycharm\bookmark-project> py manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 24, 2023 - 12:55:42
Django version 4.2.5, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
지금까지 작성한 내용을 확인하기 위해서, 프로젝트를 실행합니다. 관리자 페이지에 접속하면, 다음과 같이 새로 생성한 Bookmarks 테이블을 확인 할 수 있습니다.
*Admin (http://127.0.0.1:8000/admin) 사이트 로그인 후 첫 페이지
그림5-03-01. 관리자 bookmark 테이블 생성 화면
5-3-4. URL Config
이전 장에서 처리 흐름에 따라 URL 및 View를 설계했습니다. URL config라 함은 이런 URL 과 뷰를 연결해 주는 작업입니다. 관리자 페이지를 포함해서 5개의 URL 과 뷰가 필요합니다.
windows command
C:\hyungsik74\pycharm\bookmark-project\config> cd
C:\hyungsik74\pycharm\bookmark-project\config
C:\hyungsik74\pycharm\bookmark-project\config> notepad urls.py
C:\hyungsik74\pycharm\bookmark-project\config> cd ..\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd
C:\hyungsik74\pycharm\bookmark-project\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> notepad urls.py
linux command
$/home/hyungsik74/pycharm/bookmark-project> cd /home/hyungsik74/pycharm/bookmark-project/config
$/home/hyungsik74/pycharm/bookmark-project/confing> pwd
/home/hyungsik74/pycharm/bookmark-project/config
$/home/hyungsik74/pycharm/bookmark-project/config> vi urls.py
$/home/hyungsik74/pycharm/bookmark-project> cd /home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> pwd
/home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> vi urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("bookmark/", include("bookmark.urls")),
path("admin/", admin.site.urls),
]
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("bookmark/", include("bookmark.urls")),
path("admin/", admin.site.urls),
]
from django.urls import path
from . import views
app_name = "bookmark"
urlpatterns = [
# http://127.0.0.1/bookmark/
path("", views.ListView.as_view(), name='list'),
path("add/", views.CreateView.as_view(), name='add'),
path("detail/<int:pk>/", views.DetailView.as_view(), name='detail'),
path("update/<int:pk>/", views.UpdateView.as_view(), name='update'),
path("delete/<int:pk>/", views.DeleteView.as_view(), name='delete'),
]
from django.urls import path
from . import views
app_name = "bookmark"
urlpatterns = [
# http://127.0.0.1/bookmark/
path("", views.ListView.as_view(), name='list'),
path("add/", views.CreateView.as_view(), name='add'),
path("detail/<int:pk>/", views.DetailView.as_view(), name='detail'),
path("update/<int:pk>/", views.UpdateView.as_view(), name='update'),
path("delete/<int:pk>/", views.DeleteView.as_view(), name='delete'),
]
5-3-5. Tempate
이번 장에서는 하면 UI 에 해당하는 템플릿을 작성하겠습니다.
-- templates > bookmark_list.html 작성
windows command
C:\hyungsik74\pycharm\bookmark-project> cd C:\hyungsik74\pycharm\bookmark-project\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd
C:\hyungsik74\pycharm\bookmark-project\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> mkdir templates
C:\hyungsik74\pycharm\bookmark-project\bookmark> mkdir templates\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd templates\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark\templates\bookmark> notepad bookmark_list.html
linux command
$/home/hyungsik74/pycharm/bookmark-project> cd /home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> pwd
/home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> mkdir templates
$/home/hyungsik74/pycharm/bookmark-project/bookmark> mkdir templates/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> cd templates/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark/templates/bookmark> vi bookmark_list.html
{% block title %}Bookmark List{% endblock %}
{% block content %}
<div class="btn-group">
<a href="{% url 'bookmark:add' %}" class="btn btn-info">Add Bookmark</a>
</div>
<p></p>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Site</th>
<th scope="col">URL</th>
<th scope="col">Modify</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{% for bookmark in object_list %}
<tr>
<td>{{forloop.counter}}</td>
<td><a href="{% url 'bookmark:detail' pk=bookmark.id %}">{{bookmark.site_name}}</a></td>
<td><a href="{{bookmark.url}}" target="_blank">{{bookmark.url}}</a></td>
<td><a href="{% url 'bookmark:update' pk=bookmark.id %}" class="btn btn-success btn-sm">Modify</a></td>
<td><a href="{% url 'bookmark:delete' pk=bookmark.id %}" class="btn btn-danger btn-sm">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
{% block title %}Bookmark List{% endblock %}
{% block content %}
<div class="btn-group">
<a href="{% url 'bookmark:add' %}" class="btn btn-info">Add Bookmark</a>
</div>
<p></p>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Site</th>
<th scope="col">URL</th>
<th scope="col">Modify</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{% for bookmark in object_list %}
<tr>
<td>{{forloop.counter}}</td>
<td><a href="{% url 'bookmark:detail' pk=bookmark.id %}">{{bookmark.site_name}}</a></td>
<td><a href="{{bookmark.url}}" target="_blank">{{bookmark.url}}</a></td>
<td><a href="{% url 'bookmark:update' pk=bookmark.id %}" class="btn btn-success btn-sm">Modify</a></td>
<td><a href="{% url 'bookmark:delete' pk=bookmark.id %}" class="btn btn-danger btn-sm">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
-- templates > bookmark_create.html 작성
windows command
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd templates\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark\templates\bookmark> notepad bookmark_create.html
linux command
$/home/hyungsik74/pycharm/bookmark-project/bookmark> cd templates/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark/templates/bookmark> vi bookmark_create.html
{% block title %}Bookmark Add{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Add" class="btn btn-info btn-sm">
</form>
{% endblock %}
{% block title %}Bookmark Add{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Add" class="btn btn-info btn-sm">
</form>
{% endblock %}
-- templates > bookmark_detail.html 작성
windows command
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd templates\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark\templates\bookmark> notepad bookmark_detail.html
linux command
$/home/hyungsik74/pycharm/bookmark-project/bookmark> cd templates/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark/templates/bookmark> vi bookmark_detail.html
{% block title %}Detail{% endblock %}
{% block content %}
{{object.site_name}}<br/>
{{object.url}}
{% endblock %}
{% block title %}Detail{% endblock %}
{% block content %}
{{object.site_name}}<br/>
{{object.url}}
{% endblock %}
-- templates > bookmark_update.html 작성
windows command
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd templates\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark\templates\bookmark> notepad bookmark_update.html
linux command
$/home/hyungsik74/pycharm/bookmark-project/bookmark> cd templates/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark/templates/bookmark> vi bookmark_update.html
{% block title %}Bookmark Add{% endblock %}
{% block content %}
<form action="{% url 'bookmark:update' bookmark.id %}" method="post">
{% csrf_token %}
{{form.as_p}}
<!-- {{form}} -->
<input type="submit" value="Update" class="btn btn-info btn-sm">
</form>
{% endblock %}
{% block title %}Bookmark Add{% endblock %}
{% block content %}
<form action="{% url 'bookmark:update' bookmark.id %}" method="post">
{% csrf_token %}
{{form.as_p}}
<!-- {{form}} -->
<input type="submit" value="Update" class="btn btn-info btn-sm">
</form>
{% endblock %}
-- templates > bookmark_confirm_delete.html 작성
windows command
C:\hyungsik74\pycharm\bookmark-project\bookmark\templates\bookmark> notepad bookmark_confirm_delete.html
linux command
$/home/hyungsik74/pycharm/bookmark-project/bookmark/templates/bookmark> vi bookmark_confirm_delete.html
{% block title %}Confirm Delete{% endblock %}
{% block content %}
<form action="" method="post">
{%csrf_token%}
<div class="aler alert-danger">Do you want to delete Bookmark "{{object}}"?</div>
<input type="submit" value="Delete" class="btn btn-danger">
</form>
{% endblock %}
{% block title %}Confirm Delete{% endblock %}
{% block content %}
<form action="" method="post">
{%csrf_token%}
<div class="aler alert-danger">Do you want to delete Bookmark "{{object}}"?</div>
<input type="submit" value="Delete" class="btn btn-danger">
</form>
{% endblock %}
5-3-6. View
뷰는 bookmark/views.py 파일에서 아래의 5개 class 뷰로 작성합니다.
- ListView
- CreateView
- DetailView
- UpdateView
- DeleteView
windows command
C:\hyungsik74\pycharm\bookmark-project\> cd C:\hyungsik74\pycharm\bookmark-project\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> cd
C:\hyungsik74\pycharm\bookmark-project\bookmark
C:\hyungsik74\pycharm\bookmark-project\bookmark> notepad views.py
linux command
$/home/hyungsik74/pycharm/bookmark-project> cd /home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> pwd
/home/hyungsik74/pycharm/bookmark-project/bookmark
$/home/hyungsik74/pycharm/bookmark-project/bookmark> vi views.py
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class ListView(generic.ListView):
model = Bookmark
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class ListView(generic.ListView):
model = Bookmark
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class CreateView(generic.CreateView):
model = Bookmark
fields = ['site_name','url']
success_url = reverse_lazy('bookmark:list')
template_name_suffix = '_create'
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class CreateView(generic.CreateView):
model = Bookmark
fields = ['site_name','url']
success_url = reverse_lazy('bookmark:list')
template_name_suffix = '_create'
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class DetailView(generic.DetailView):
model = Bookmark
template_name_suffix = '_detail'
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class DetailView(generic.DetailView):
model = Bookmark
template_name_suffix = '_detail'
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class UpdateView(generic.UpdateView):
model = Bookmark
fields = ['site_name','url']
template_name_suffix = '_update'
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class UpdateView(generic.UpdateView):
model = Bookmark
fields = ['site_name','url']
template_name_suffix = '_update'
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class DeleteView(generic.DeleteView):
model = Bookmark
success_url = reverse_lazy('bookmark:list')
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class DeleteView(generic.DeleteView):
model = Bookmark
success_url = reverse_lazy('bookmark:list')
# Create your views here.
# CRUD : Create, Read, Update, Delete
# List
# 클래스형 뷰, 함수형 뷰
# 웹 페이지에 접속한다. -> 페이지를 본다.
# URL을 입력 -> 웹 서버가 뷰를 찾아서 동작시킨다. -> 응답
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class ListView(generic.ListView):
model = Bookmark
class CreateView(generic.CreateView):
model = Bookmark
fields = ['site_name','url']
success_url = reverse_lazy('bookmark:list')
template_name_suffix = '_create'
class DetailView(generic.DetailView):
model = Bookmark
template_name_suffix = '_detail'
class UpdateView(generic.UpdateView):
model = Bookmark
fields = ['site_name','url']
template_name_suffix = '_update'
class DeleteView(generic.DeleteView):
model = Bookmark
success_url = reverse_lazy('bookmark:list')
# Create your views here.
# CRUD : Create, Read, Update, Delete
# List
# 클래스형 뷰, 함수형 뷰
# 웹 페이지에 접속한다. -> 페이지를 본다.
# URL을 입력 -> 웹 서버가 뷰를 찾아서 동작시킨다. -> 응답
from django.views import generic
from django.urls import reverse_lazy
from .models import Bookmark
class ListView(generic.ListView):
model = Bookmark
class CreateView(generic.CreateView):
model = Bookmark
fields = ['site_name','url']
success_url = reverse_lazy('bookmark:list')
template_name_suffix = '_create'
class DetailView(generic.DetailView):
model = Bookmark
template_name_suffix = '_detail'
class UpdateView(generic.UpdateView):
model = Bookmark
fields = ['site_name','url']
template_name_suffix = '_update'
class DeleteView(generic.DeleteView):
model = Bookmark
success_url = reverse_lazy('bookmark:list')
5-3-7. 실행하기
TIP
장고 웹 프레임워크를 사용해서 프로젝트를 처음 생성할 때 마다 반듯이 필요한 절차 이므로 꼭 이해하고, 익숙해져야 합니다.
windows command
C:\hyungsik74\pycharm\bookmark-project> cd C:\hyungsik74\pycharm\bookmark-project
C:\hyungsik74\pycharm\bookmark-project> cd
C:\hyungsik74\pycharm\bookmark-project
C:\hyungsik74\pycharm\bookmark-project> py manage.py runserver
linux command
$/home/hyungsik74/pycharm/bookmark-project> cd /home/hyungsik74/pycharm/bookmark-project
$/home/hyungsik74/pycharm/bookmark-project> pwd
/home/hyungsik74/pycharm/bookmark-project
$/home/hyungsik74/pycharm/bookmark-project> python manage.py runserver
- 브라우저에서 http://127.0.0.1:8000/bookmark 접속합니다.
그림5-3-7. 사용자 어플리케이션 초기 화면