MTV ํจํด์์์ View
View๋ Django์ MTV(Model-Template-View) ํจํด์์ ํต์ฌ์ ์ธ ์ญํ ์ ๋ด๋นํ๋ ์ปดํฌ๋ํธ์
๋๋ค.
โข
ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ์ ์ฒ๋ฆฌ
โข
Model๊ณผ ์ํธ์์ฉํ์ฌ ๋ฐ์ดํฐ ์กฐ์
โข
๋น์ฆ๋์ค ๋ก์ง ์ฒ๋ฆฌ
โข
Template์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ์ฌ ์๋ต ์์ฑ
View์ ์ข ๋ฅ
Django์์๋ ๋ ๊ฐ์ง ํ์
์ View๋ฅผ ์ ๊ณตํฉ๋๋ค:
โข
ํจ์ ๊ธฐ๋ฐ ๋ทฐ
โข
ํด๋์ค ๊ธฐ๋ฐ ๋ทฐ
1. ํจ์ ๊ธฐ๋ฐ ๋ทฐ (Function Based Views)
# views.py
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts})
Python
๋ณต์ฌ
2. ํด๋์ค ๊ธฐ๋ฐ ๋ทฐ (Class Based Views)
# views.py
from django.views.generic import ListView
from .models import Post
class PostListView(ListView):
model = Post
template_name = 'blog/post_list.html'
context_object_name = 'posts'
Python
๋ณต์ฌ
URL ์ค์
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('posts/', views.post_list, name='post_list'),
path('posts/class/', views.PostListView.as_view(), name='post_list_class'),
]
Python
๋ณต์ฌ
View์ ์ฃผ์ ํน์ง
ํน์ง | ์ค๋ช
|
์์ฒญ ์ฒ๋ฆฌ | HTTP ์์ฒญ(GET, POST ๋ฑ)์ ์ฒ๋ฆฌํ๊ณ ์ ์ ํ ์๋ต์ ๋ฐํ |
๋ฐ์ดํฐ ์ฒ๋ฆฌ | Model์ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์
์ํ |
๋น์ฆ๋์ค ๋ก์ง | ์ ํ๋ฆฌ์ผ์ด์
์ ํต์ฌ ๋ก์ง์ ๊ตฌํ |
์๋ต ์์ฑ | Template๊ณผ ๋ฐ์ดํฐ๋ฅผ ๊ฒฐํฉํ์ฌ ์ต์ข
์๋ต ์์ฑ |
์ค์ ํ๋ก์ ํธ ์์
# views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Article
from .forms import ArticleForm
@login_required
def article_create(request):
if request.method == 'POST':
form = ArticleForm(request.POST)
if form.is_valid():
article = form.save(commit=False)
article.author = request.user
article.save()
return redirect('article_detail', pk=article.pk)
else:
form = ArticleForm()
return render(request, 'blog/article_form.html', {'form': form})
def article_detail(request, pk):
article = get_object_or_404(Article, pk=pk)
return render(request, 'blog/article_detail.html', {'article': article})
Python
๋ณต์ฌ
์ด ์์์์๋ ๊ธ ์์ฑ๊ณผ ์์ธ ๋ณด๊ธฐ ๊ธฐ๋ฅ์ ๊ตฌํํ View๋ฅผ ๋ณด์ฌ์ค๋๋ค. article_create ๋ทฐ๋ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ง ์ ๊ทผํ ์ ์์ผ๋ฉฐ, POST ์์ฒญ ์ ์ ๊ธ์ ์์ฑํ๊ณ GET ์์ฒญ ์ ํผ์ ํ์ํฉ๋๋ค.
View ์์ฑ ์ ๊ณ ๋ ค์ฌํญ
โข
๋ณด์: ์ ์ ํ ์ธ์ฆ/์ธ๊ฐ ์ฒ๋ฆฌ
โข
์ฑ๋ฅ: ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฟผ๋ฆฌ ์ต์ ํ
โข
์ฌ์ฌ์ฉ์ฑ: ๊ณตํต ๋ก์ง์ ๋ชจ๋ํ
โข
์ ์ง๋ณด์์ฑ: ๋ช
ํํ ์ฝ๋ ๊ตฌ์กฐ์ ๋ฌธ์ํ
from django.shortcuts
Django์์ from django.shortcuts๋ก ๊ฐ์ ธ์ค๋ ํจ์๋ค์ ๋ทฐ(view) ํจ์ ๋ด์์ ์์ฃผ ์ฌ์ฉํ๋ ํฌํผ ํจ์๋ค์
๋๋ค. ์๋๋ ๊ฐ์ฅ ๋ง์ด ์ฐ์ด๋ ์ฃผ์ ํจ์๋ค๊ณผ ์ค๋ช
, ์ฌ์ฉ๋ฒ์
๋๋ค.
django.shortcuts ์ฃผ์ ํจ์ ์์ฝ
ํจ์๋ช
| ์ค๋ช
|
render() | ํ
ํ๋ฆฟ์ ๋ ๋๋งํ์ฌ HTTP ์๋ต ๋ฐํ |
redirect() | ๋ค๋ฅธ URL๋ก ๋ฆฌ๋๋ ์
|
get_object_or_404() | ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์์ผ๋ฉด 404 ์๋ฌ ๋ฐ์ |
get_list_or_404() | ๊ฐ์ฒด ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๊ณ , ์์ผ๋ฉด 404 ์๋ฌ ๋ฐ์ |
๊ฐ ํจ์ ์์ธ ์ค๋ช
๋ฐ ์ฌ์ฉ๋ฒ
1. render()
render(request, template_name, context=None, content_type=None, status=None)
โข
ํ
ํ๋ฆฟ์ ๋ ๋๋งํ์ฌ HttpResponse ๊ฐ์ฒด๋ฅผ ๋ฐํ
โข
๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ํ
ํ๋ฆฟ ๋ ๋๋ง ๋ฐฉ์
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {'title': '๋ฉ์ธ ํ์ด์ง'})
Python
๋ณต์ฌ
2. redirect
redirect(to, *args, **kwargs)
Django redirect() 3๊ฐ์ง ๋ฐฉ๋ฒ ์ ๋ฆฌ
1.
URL
2.
๋ทฐ ์ด๋ฆ
3.
ํจํด ์ด๋ฆ
๊ธฐ๋ณธ ๋ฌธ๋ฒ
from django.shortcuts import redirect
redirect(to, *args, **kwargs)
Python
๋ณต์ฌ
1. URL ๋ฌธ์์ด๋ก ๋ฆฌ๋๋ ์
return redirect('/post/')
Python
๋ณต์ฌ
โข
๋จ์ํ ๊ฒฝ๋ก ๋ฌธ์์ด๋ก ์ด๋
โข
๋จ์ : URL์ด ๋ณ๊ฒฝ๋๋ฉด ์ง์ ์์ ํด์ผ ํจ โ ์ ์ง๋ณด์ ์ด๋ ค์
2. ๋ทฐ ํจ์(๋๋ ํด๋์ค) ์ด๋ฆ์ผ๋ก ๋ฆฌ๋๋ ์
return redirect(my_view)
Python
๋ณต์ฌ
โข
urls.py์ ๋ฑ๋ก๋ ๋ทฐ ํจ์๋ ํด๋์ค ์ด๋ฆ์ ์ง์ ์ง์
โข
๋ด๋ถ์ ์ผ๋ก reverse() ์ฌ์ฉ
โข
์ ์ฐ์ด์ง ์์ผ๋ฉฐ, ๋๋ถ๋ถ URL name ๋ฐฉ์์ด ๋ ๋ง์ด ์ฌ์ฉ๋จ
3. URL ํจํด์ name ๊ฐ์ผ๋ก ๋ฆฌ๋๋ ์ (๊ถ์ฅ)
return redirect('post_list') # ๋จ์ ์ด๋
return redirect('post_detail', pk=5) # ์ธ์ ์ ๋ฌ
Python
๋ณต์ฌ
โข
urls.py์์ ์ง์ ํ name='post_list' ๋ฑ์ ๊ธฐ์ค์ผ๋ก URL ์ญ์ถ์
โข
๋ด๋ถ์ ์ผ๋ก reverse() ์ฌ์ฉ
โข
URL์ด ๋ฐ๋์ด๋ name ์ ์งํ๋ฉด ์ฝ๋ ๋ณ๊ฒฝ ๋ถํ์ โ ์ ์ง๋ณด์์ ๊ฐ์ฅ ์ ๋ฆฌ
์์
# urls.py
path('post/', views.post_list, name='post_list')
path('post/<int:pk>/', views.post_detail, name='post_detail')
# views.py
return redirect('post_list') # ๋ชฉ๋ก ํ์ด์ง๋ก ์ด๋
return redirect('post_detail', pk=5) # ํน์ ๊ฒ์๊ธ ์์ธ๋ก ์ด๋
Python
๋ณต์ฌ
์์ฝ
๋ฐฉ์ | ๋ฌธ๋ฒ ์์ | ํน์ง |
URL ๋ฌธ์์ด | redirect('/post/') | URL ๋ณ๊ฒฝ ์ ์ง์ ์์ ํ์ |
๋ทฐ ํจ์/ํด๋์ค ์ด๋ฆ | redirect(my_view) | ๋ทฐ ์ง์ ์ง์ , ์ ์ฐ์ด์ง ์์ |
URL name (name=) | redirect('post_list') |
redirect()์์ ํ๋ผ๋ฏธํฐ ๋๊ธฐ๋ ๋ฐฉ๋ฒ
Django์์ redirect()๋ฅผ ์ฌ์ฉํ ๋ ํ๋ผ๋ฏธํฐ๋ฅผ ํจ๊ป ๋๊ธฐ๋ ๋ฐฉ๋ฒ์ urls.py์ ์ ์๋ URL ํจํด ์ธ์(args, kwargs) ๋ฅผ ํ์ฉํ๋ ๋ฐฉ์์
๋๋ค.
1. redirect('url_name', ์ธ์๋ค...)
โข
URL ํจํด์ ์ ์๋ ๋ณ์์ ๋ง์ถฐ args ๋๋ kwargs๋ก ์ ๋ฌ
urls.py ์์
from django.urls import path
from . import views
urlpatterns = [
path('post/<int:pk>/', views.post_detail, name='post_detail'),
]
Python
๋ณต์ฌ
views.py ์์
๋ฐฉ๋ฒ 1: kwargs ์ฌ์ฉ (pk=5)
from django.shortcuts import redirect
def my_view(request):
return redirect('post_detail', pk=5)
Python
๋ณต์ฌ
๋ฐฉ๋ฒ 2: args ์ฌ์ฉ (์์๋๋ก)
def my_view(request):
return redirect('post_detail', 5)
Python
๋ณต์ฌ
์ถ๊ฐ ํ
โข
redirect()๋ ๋ด๋ถ์ ์ผ๋ก reverse()๋ฅผ ์ฌ์ฉํฉ๋๋ค.
โข
์ง์ reverse()๋ก URL ๋ฌธ์์ด์ ์์ฑํ๊ณ ๋๊ธฐ๋ ๊ฒ๋ ๊ฐ๋ฅ:
from django.urls import reverse
url = reverse('post_detail', kwargs={'pk': 5})
return redirect(url)
Python
๋ณต์ฌ
์์ฝ ํ
๋ฐฉ๋ฒ | ์์ | ์ค๋ช
|
redirect('url_name', pk=5) | URL name + ์ธ์ ์ ๋ฌ | |
redirect('url_name', 5) | ๊ฐ๋ฅ | ์์ ์ฃผ์ (args) |
redirect(reverse('url_name', kwargs={...})) | ๊ณ ๊ธ | URL ๋ฌธ์์ด ์์ฑ ํ ์ ๋ฌ |
์ฟผ๋ฆฌ์คํธ๋ง ๋๊ธฐ๋ ๋ฐฉ๋ฒ
Django์์ redirect()๋ก ๋ฆฌ๋๋ ์
ํ ๋ ์ฟผ๋ฆฌ์คํธ๋ง(์: ?page=2&search=hello)์ ํจ๊ป ๋๊ธฐ๋ ค๋ฉด, redirect()์๋ ์ฟผ๋ฆฌ ๋ฌธ์์ด์ด ํฌํจ๋ ์์ ํ URL์ ์ง์ ์ ๋ฌํด์ผ ํฉ๋๋ค.
from django.shortcuts import redirect
def my_view(request):
return redirect('/posts/?page=2&search=hello')
Python
๋ณต์ฌ
reverse()์ ํจ๊ป ์ฟผ๋ฆฌ์คํธ๋ง ์ถ๊ฐ
from django.shortcuts import redirect
from django.urls import reverse
def my_view(request):
base_url = reverse('post_list') # ex. '/posts/'
query_string = '?page=2&search=hello'
return redirect(base_url + query_string)
Python
๋ณต์ฌ
urllib.parse๋ก ์์ ํ๊ฒ ์ฟผ๋ฆฌ ์ถ๊ฐ
from django.shortcuts import redirect
from django.urls import reverse
from urllib.parse import urlencode
def my_view(request):
base_url = reverse('post_list')
query_params = {'page': 2, 'search': 'hello'}
return redirect(f'{base_url}?{urlencode(query_params)}')
Python
๋ณต์ฌ
์์ฝ
๋ฐฉ์ | ์ค๋ช
| ์์ |
๋ฌธ์์ด ์ง์ | ๊ฐ๋จํ ๊ฒฝ์ฐ | redirect('/posts/?page=2') |
reverse() + ์ฟผ๋ฆฌ | URL name์ผ๋ก๋ถํฐ ์์ฑ | redirect(reverse('post_list') + '?page=2') |
urlencode() | redirect(f"{url}?{urlencode(params)}") |
3. get_object_or_404()
get_object_or_404(klass, *args, **kwargs)
โข
๊ฐ์ฒด๊ฐ ์กด์ฌํ๋ฉด ๋ฐํ, ์์ผ๋ฉด Http404 ์์ธ ๋ฐ์
from django.shortcuts import get_object_or_404
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'post/detail.html', {'post': post})
Python
๋ณต์ฌ
get_object_or_404() vs Model.objects ๋น๊ต
๋ ๋ฐฉ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๊ฐ์ฒด๋ฅผ ์กฐํํ๋ ๋ฐฉ๋ฒ์ด์ง๋ง, ๊ฐ์ฒด๊ฐ ์์ ๋์ ์ฒ๋ฆฌ ๋ฐฉ์์ด ๋ค๋ฆ
๋๋ค.
ํน์ง | get_object_or_404() | Model.objects |
์๋ ๊ฐ์ฒด ์กฐํ ์ | ์๋์ผ๋ก 404 ํ์ด์ง ํ์ | DoesNotExist ์์ธ ๋ฐ์ |
์์ธ ์ฒ๋ฆฌ | ์๋ ์ฒ๋ฆฌ | ์๋์ผ๋ก try-except ํ์ |
์ฌ์ฉ context | ์น ๋ทฐ์์ ๊ถ์ฅ | ์ผ๋ฐ์ ์ธ ํ์ด์ฌ ์ฝ๋ |
Model.objects ์ฌ์ฉ ์์
try:
post = Post.objects.get(pk=pk)
except Post.DoesNotExist:
raise Http404("Post does not exist")
return render(request, 'post/detail.html', {'post': post})
Python
๋ณต์ฌ
get_object_or_404() ์ฌ์ฉ ์์
post = get_object_or_404(Post, pk=pk)
return render(request, 'post/detail.html', {'post': post})
Python
๋ณต์ฌ
โข
get_object_or_404() ์ฅ์ : ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ , HTTP 404 ์๋ต์ ์๋์ผ๋ก ์ฒ๋ฆฌ
โข
Model.objects ์ฅ์ : ๋ ์ ์ฐํ ์์ธ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๊ณ , ์น ์ปจํ
์คํธ ์ธ๋ถ์์๋ ์ฌ์ฉ ๊ฐ๋ฅ
4. get_list_or_404()
get_list_or_404(klass, *args, **kwargs)
โข
์ฟผ๋ฆฌ์
์ ๋ฆฌ์คํธ๋ก ๋ฐํ, ๊ฒฐ๊ณผ๊ฐ ์์ผ๋ฉด Http404 ๋ฐ์
from django.shortcuts import get_list_or_404
def user_posts(request, username):
posts = get_list_or_404(Post, user__username=username)
return render(request, 'post/user_posts.html', {'posts': posts})
Python
๋ณต์ฌ
get_list_or_404() vs Model.objects.all() ๋น๊ต
๋ ๋ฐฉ์์ ๊ฐ์ฒด ๋ชฉ๋ก์ ์กฐํํ๋ ๋ฐฉ๋ฒ์ด์ง๋ง, ๊ฒฐ๊ณผ๊ฐ ์์ ๋์ ์ฒ๋ฆฌ ๋ฐฉ์์ด ๋ค๋ฆ
๋๋ค.
ํน์ง | get_list_or_404() | Model.objects.all() |
๊ฒฐ๊ณผ๊ฐ ์์ ๋ | ์๋์ผ๋ก 404 ํ์ด์ง ํ์ | ๋น ์ฟผ๋ฆฌ์
๋ฐํ |
์์ธ ์ฒ๋ฆฌ | ์๋ ์ฒ๋ฆฌ | ์๋์ผ๋ก ์ฒ๋ฆฌ ํ์ |
์ฌ์ฉ context | ์น ๋ทฐ์์ ๊ถ์ฅ | ์ผ๋ฐ์ ์ธ ํ์ด์ฌ ์ฝ๋ |
Model.objects.all() ์ฌ์ฉ ์์
def user_posts(request, username):
posts = Post.objects.filter(user__username=username)
if not posts.exists():
# ์๋์ผ๋ก 404 ์ฒ๋ฆฌ
raise Http404("Posts not found")
return render(request, 'post/user_posts.html', {'posts': posts})
Python
๋ณต์ฌ
get_list_or_404() ์ฌ์ฉ ์์
def user_posts(request, username):
posts = get_list_or_404(Post, user__username=username)
return render(request, 'post/user_posts.html', {'posts': posts})
Python
๋ณต์ฌ
โข
get_list_or_404() ์ฅ์ : ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ , HTTP 404 ์๋ต์ ์๋์ผ๋ก ์ฒ๋ฆฌ
โข
Model.objects.all() ์ฅ์ : ๋ ์ ์ฐํ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๊ณ , ์น ์ปจํ
์คํธ ์ธ๋ถ์์๋ ์ฌ์ฉ ๊ฐ๋ฅ
์์ฝ ํ
ํจ์ | ์ฃผ์ ์ฉ๋ |
render() | ํ
ํ๋ฆฟ ๋ ๋๋ง |
redirect() | URL ๋ฆฌ๋๋ ์
|
get_object_or_404() | ๋จ์ผ ๊ฐ์ฒด ๊ฐ์ ธ์ค๊ธฐ (์์ผ๋ฉด 404) |
get_list_or_404() | ๊ฐ์ฒด ๋ฆฌ์คํธ ๊ฐ์ ธ์ค๊ธฐ (์์ผ๋ฉด 404) |