Search

View

MTV ํŒจํ„ด์—์„œ์˜ View

View๋Š” Django์˜ MTV(Model-Template-View) ํŒจํ„ด์—์„œ ํ•ต์‹ฌ์ ์ธ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.
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
๋ณต์‚ฌ
์ฃผ์˜: args๋Š” URL ํŒจํ„ด์— ์„ ์–ธ๋œ ์ˆœ์„œ์™€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋…์„ฑ๊ณผ ๋ช…ํ™•์„ฑ์„ ์œ„ํ•ด kwargs ๋ฐฉ์‹์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ํŒ

โ€ข
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)