FBV (Function Based View)๋ก ๋ธ๋ก๊ทธ ๋ฆฌ์คํธ ํ์ด์ง ๋ง๋ค๊ธฐ
๊ธฐ๋ณธ์ ์ผ๋ก ํ๋ก์ ํธ ํด๋์ ์๋ urls.py
์๋ admin/
๋ง ๋ฑ๋ก๋์ด ์๋ค. ๋ฐ๋ผ์ ๋ค์๊ณผ ๊ฐ์ด 127.0.0.1/blog
์ฃผ์๋ก ๋ค์ด๊ฐ๋ฉด 404 ์ค๋ฅ๊ฐ ๋๋ค.
urls.py
์ ์๋ urlpatterns
์ ์ด์ ํ์ ์๋ฏธ. ๋์๊ฐ ์ ์๋ ๋งํฌ๋ค์ ์๋ฏธํ๋ค. ์ฌ๊ธฐ์ blog/
๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
urls.py
Copy from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
path('admin/', admin.site.urls),
]
์ด ๋ include ๋ผ๋ ํจ์๋ฅผ import ํด์ค์ผ ํ๋ค
blog ์ฑ ํด๋์๋ ์์ง urls ๋ผ๋ ํ์ผ์ด ์๋ค. ๋ฐ๋ผ์ ์ด๋ฅผ ์ถ๊ฐํด์ค์ผ ํ๋ค.
blog/urls.py
Copy from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
์ด ๋ views.index
๋ฅผ ํธ์ถํ๋ฏ๋ก views.py
์์ index
ํจ์๋ฅผ ์ ์ธํด์ค์ผ ํ๋ค.
์ง๊ธ์ฒ๋ผ ํ๋ ๋ฐฉ๋ฒ์ด FBV ๋ฐฉ๋ฒ
blog/views.py
Copy from django.shortcuts import render
def index(request):
return render(
request,
'blog/index.html',
)
blog์ index.html์ ๊ฐ์ง๊ณ ๋ ๋๋ง ํ๋ผ๋ ๋ป
๋ฐ๋ผ์ index.html์ด ๋ ํ์ํ๋ค.
templates ๋ผ๋ ํด๋ ์์, blog ๋ผ๋ ํด๋ ์์ index.html์ ๋ง๋ ๋ค
templates/blog/index.html
์ฑ์ ๋
๋ฆฝ์ฑ์ ์ํด ์ด๋ ๊ฒ ์์
ํ๋ฉฐ ์ถํ์ ์ถ๊ฐ ์ค๋ช
blog/templates/blog/index.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>blog</h1>
</body>
</html>
์ง๊ธ๊น์ง ํ ์์
์ ํด๋ผ์ด์ธํธ์์ View๋ก ์์ฒญ์ํ๊ณ View๊ฐ ํ
ํ๋ฆฟ์ ๋ ๋๋ง ํ ํ ์๋ต์ ์ค ๊ณผ์ ์ด๋ค.
์ด์ Model์์ DB๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ ๋ฆฐ ํ ๋ฐ์ดํฐ๋ฅผ ์ป์ด์ ์๋ต์ ์ฃผ๋ ๊ณผ์ ์ ํด๋ณด๋ ค๊ณ ํ๋ค.
blog/views.py
Copy from django.shortcuts import render
from .models import Post
def index(request):
posts = Post.objects.all()
return render(
request,
'blog/index.html',
{
'posts' : posts,
},
)
๋ ๋๋ง์ ๋ฆฌํด๊ฐ์ผ๋ก posts ๋ผ๋ dictionary๋ฅผ ์ถ๊ฐํ๋ค.
์ด posts๋ models.py์ ์๋ post class์ด๋ค.
์ง๊ธ๋ถํฐ์ ํ์ผ๋ค์ ํน๋ณํ ๋ช
์๊ฐ ์์ผ๋ฉด ๋ชจ๋ blog ์ฑ ๋ด๋ถ์ ์๋ ํ์ผ์ด๋ค.
index.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>blog</h1>
{% for p in posts %}
<h3>{{ p }}</h3>
{% endfor %}
</body>
</html>
10-12
html์์ ๋์ ์ธ์ด๋ฅผ ์ธ ์ ์๊ฒ๋ ์ฅ๊ณ ์์ ์ง์ํ๋ค.
for๋ฌธ์ ์์ํ ๋์ ๋๋ ๋ {% %} ๋ฅผ ์จ์ค๋ค.
posts๋ views.py
์ ์๋ dictionary ๊ฐ์ผ๋ก 'posts' key์ ๋ํ value ๊ฐ๋ค์ ํ๋์ฉ <h3> ํํ๋ก ์์ฑ๋๊ฒ ํ๋ค.
์ด ๋ ์ด๋ป๊ฒ p ๋ฅผ ๋ถ๋ฅด๊ธฐ๋ง ํด๋ db์ ์ ์ฅ๋ ๋ด์ฉ์ด ๋ค ๋ฐ๊น?
์ด์ ๋ models.py
์ ์ ์๋์ด์๋ __str__ ํจ์ ๋๋ฌธ
models.py
Copy def __str__(self):
return f"{self.pk} {self.title}"
์ข ๋ ์ถ๊ฐํด๋ณด์.
index.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>blog</h1>
{% for p in posts %}
<hr/>
<h3>{{ p }}</h3>
<h4>{{ p.create_at }}</h4>
<p> {{ p.context }}</p>
{% endfor %}
</body>
</html>
๋ํ, posts์ ๊ฐ๋ค์ ํน์ ํ
์ด๋ธ๊ฐ์ผ๋ก ์ ๋ ฌํ ์ ์๋ค.
views.py
Copy # pk ์
posts = Post.objects.all().order_by('pk')
# pk ์ญ์
posts = Post.objects.all().order_by('-pk')
์ฌ๊ธฐ์ objects.all()์ ์ฟผ๋ฆฌ๋ฌธ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
FBV (Function Based View)๋ก ๋ธ๋ก๊ทธ ์์ธ ํ์ด์ง ๋ง๋ค๊ธฐ
blog ํ์ด์ง์์ ๊ฒ์๊ธ์ ๋๋ ์ ๋ ์๋ก์ด ํ์ด์ง๋ก ์ด๋ํ๋ ๊ณผ์ ์ ๊ตฌ์ฑํ ๊ฒ์
urls.py
Copy from django.urls import path
from . import views
urlpatterns = [
path('<int:pk>/', views.single_post_page)
path('', views.index),
]
<> ์์๋ ์๋ฃํ์ ๋ฃ์ ์ ์์ผ๋ฉฐ pk๊ฐ ์ ์๋ก ์ค๊ธฐ ๋๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ๋ฅ
views.py
์ single_post_page
ํจ์๋ฅผ ์ถ๊ฐํ ๊ฒ์
views.py
Copy def single_post_page(request, pk):
post = Post.objects.get(pk=pk)
return render(
request,
'blog/single_page.html',
{
'post': post,
}
)
single_page.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>{{ post.title }} - Blog</title>
</head>
<body>
<nav>
<a href="/blog/">Blog</a>
</nav>
<h1>{{ post.title }}</h1>
<h4>{{ post.created_at }}</h4>
<p>{{ post.content }}</p>
<p>์ฌ๊ธฐ์ ๋๊ธ์ด ๋ค์ด์ฌ ์ ์๊ฒ ์ฃ ?</p>
</body>
</html>
์ฌ๊ธฐ์ ๋ธ๋ก๊ทธ๋ฅผ ๋๋ฅด๋ฉด ๋ชจ๋ /blog ๋ก ์ด๋ํ๋ค.
index.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>blog</h1>
{% for p in posts %}
<hr/>
<h3>><a href="{{ p.get_absolute_url }}">{{ p }}</a></h3>
<h4>{{ p.create_at }}</h4>
<p> {{ p.context }}</p>
{% endfor %}
</body>
</html>
์ฌ๊ธฐ์ p.get_absolute_url
๋ฅผ ํธ์ถํ ์ ์๋ค. ์ด๊ฒ์ model.py
์์ ์ ์ํด์ค์ผ ํ๋ ํจ์์ด์ง๋ง ์ฅ๊ณ ์์ ์ ๊ณตํด์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค.
์ฅ๊ณ ์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ ์ ์ํด์ฃผ๋ฉด ๋๋ค๋ ๋ป
models.py
Copy def get_absolute_url(self):
return f'/blog/{self.pk}'
๊ฐ ํฌ์คํธ์์ /blog๋ก ์ด๋ํ ์ ์์ผ๋ฉฐ admin ํ์ด์ง์์๋ view on site ๊ธฐ๋ฅ์ด ์ถ๊ฐ๋์๋ค.
FBV๋ก ๋๋ฌธ ํ์ด์ง ์๊ธฐ์๊ฐํ์ด์ง ๋ง๋ค๊ธฐ
127.0.0.1/8000 ์์ blog๋ admin์ ์
๋ ฅํ์ง ์์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ๋๋ฌธ ํ์ด์ง๋ ๋ง๋ จํด์ฃผ์.
urls.py
Copy from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
path('admin/', admin.site.urls),
path('', include('single_pages.urls'))
]
single_pages/urls.py
Copy from django.urls import path
from . import views
urlpatterns = [
path('', views.landing),
]
single_pages/views.py
Copy from django.shortcuts import render
def landing(request):
return render(
request,
'single_pages/landing.html',
)
single_pages/templates/single_pages/landing.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>์๋ฏผ์ด์ ์น์ฌ์ดํธ</title>
</head>
<body>
<nav>
<a href="/blog/">Blog</a>
<a href="/about_me/">About me</a>
</nav>
<h1>์๋
ํ์ธ์. ์ ์๋ฏผ์
๋๋ค.</h1>
<h2>๋๋ฌธ ํ์ด์ง</h2>
<h3>๊ฐ๋ฐ์ค...</h3>
</body>
</html>
CBV๋ก ๋ธ๋ก๊ทธ ํฌ์คํธ ๋ชฉ๋ก ํ์ด์ง ๋ง๋ค๊ธฐ
์ฅ๊ณ ์์ ์ด๋ฌํ ๋ธ๋ก๊ทธ ํ์ด์ง๋ค์ ํด๋์ค๋ก ์ ๊ณตํ๊ณ ์๋ค.
blog/urls.py
Copy from django.urls import path
from . import views
urlpatterns = [
path('<int:pk>/', views.single_post_page),
path('', views.PostList.as_view())
# path('', views.index),
]
blog/views.py
Copy from django.shortcuts import render
from django.views.generic import ListView
from .models import Post
class PostList(ListView):
model = Post
template_name = "blog/index.html"
# def index(request):
# posts = Post.objects.all().order_by('pk')
# return render(
# request,
# 'blog/index.html',
# {
# 'posts' : posts,
# },
# )
์ด ๋ ์๋ฒ๋ฅผ ์คํํด๋ณด๋ฉด,
์๋ฌด๊ฒ๋ ๋จ์ง ์๋๋ค. ์ด๋,
index.html
Copy <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>blog</h1>
{% for p in posts %}
<hr/>
<h3>><a href="{{ p.get_absolute_url }}">{{ p }}</a></h3>
<h4>{{ p.create_at }}</h4>
<p> {{ p.context }}</p>
{% endfor %}
</body>
</html>
๋ฐ๋ณต๋ฌธ์์ posts์ ํด๋นํ๋ dictionary๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ฅ๊ณ ์์ ์ ๊ณตํ๋ CBV๋ dictionary ๋ณ์๋ฅผ post_list
๋ก ์ ์ํ๋ค. ๋ฐ๋ผ์ ์ด๋ ๊ฒ ์์ ํด์ฃผ๋ฉด ๋๋ค.
Copy {% for p in post_list %}
๋ํ, views.py ์์ template_name์ ์ ์ธํด์ฃผ์ง ์๊ณ template ํ์ผ ์ด๋ฆ์ post_list
๋ก ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋๋ค.
๋, ์์๋ ์ ํด์ค ์ ์๋ค.
views.py
Copy class PostList(ListView):
model = Post
ordering = '-pk'
CBV๋ก ๋ธ๋ก๊ทธ ํฌ์คํธ ์์ธ ํ์ด์ง ๋ง๋ค๊ธฐ
๋ง์ฐฌ๊ฐ์ง๋ก single_page๋ ํด๋์ค๋ก ์ ์ธ ๊ฐ๋ฅํ๋ค.
views.py
Copy from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Post
class PostDetail(DetailView):
model = Post
# def single_post_page(request, pk):
# post = Post.objects.get(pk=pk)
# return render(
# request,
# 'blog/single_page.html',
# {
# 'post': post,
# }
# )
๊ทธ๋ฆฌ๊ณ ํ
ํ๋ฆฟ ํ์ผ ์ด๋ฆ์ post_detail.html
๋ก ๋ณ๊ฒฝํ๋ค.
๋ง์ง๋ง์ผ๋ก, url๋ ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋.
urls.py
Copy from django.urls import path
from . import views
urlpatterns = [
path('<int:pk>/', views.PostDetail.as_view()),
# path('<int:pk>/', views.single_post_page),
path('', views.PostList.as_view())
# path('', views.index),
]
์ฅ๊ณ ์์๋ CBV๋ก ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ด ๋ง๊ธฐ ๋๋ฌธ์ ํธ๋ฆฌํ๊ฒ ์ด์ฉํ ์ ์๊ณ ์ด๊ฒ์ ์ง์ ๊ตฌํํ๊ธฐ๋ ์๋ชจ์ ์ด๊ธฐ ๋๋ฌธ์ CBV๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค!