오식랜드
[django] FBV / CBV 방식 본문
반응형
url 설정 : 앱/urls.py
from django.urls import path
from . import views
urlpatterns = [
# CBV 방식 (class)
path('', views.PostList.as_view()),
path('/<int:pk>', views.PostDetail.as_view()),
# FBV (Function) 방식
path('', views.index),
path('/<int:pk>', views.single_page_post),
]
render 설정 : 앱/views.py
from django.shortcuts import render
from .models import DB모델명
################# CBV 방식 (class) #################
from django.views.generic import ListView, DetailView
class PostList(ListView):
model = DB모델명
ordering = '-pk'
class PostDetail(DetailView):
model = DB모델명
################# FBV (Function) 방식 #################
def index(req):
posts = DB모델명.objects.all().order_by('-pk')
return render(
req,
"blog/index.html",
{
'posts': posts
}
)
def single_page_post(req, pk):
post = DB모델명.objects.get(pk=pk)
return render(
req,
"blog/single_page_post.html",
{
'post': post
}
)
templates 설정
- 앱/templates/앱이름 폴더 생성 + 그 안에 html 파일 생성
- html 에서 데이터 사용
# 상세페이지 <nav> <a href="/blog">Bolg</a> </nav> <h1>{{ post.title }}</h1> <h4>{{ post.create_at }}</h4> <p>{{ post.content }}</p> <hr> <h3>댓글창</h3>
- # 글 목록 {% for p in posts %} <hr> <h2> <a href="{{p.get_absolute_url}}"> {{ p.title }} </a> </h2> <h3>{{ p.create_at }}</h3> <p>{{ p.content }}</p> {% endfor %}
- bootstrap 불러오기
- 폴더 생성 : 앱/static/앱이름/폴더명
- html 시작하며 static 불러오기
<!DOCTYPE html> <html> {% load static %}
- bootstrap 링크시키기 : 경로는 static 이후로 작성
<link href="{% static 'blog/bootstrap/bootstrap.min.css' %}" rel="stylesheet" type="text/css" media="screen">
Template 문법
Template 태그 :
{% 문법 %} {% for in %} ~ {% endfor %}
{% if %} .. {% elif %} .. {% else %} .. {% endif %}
{% with 변수명 = '값' %}
{% csrf_token %}
Template 변수 : {{ 변수 }}
Template 필터 : {{ 변수|옵션 }}
적용 예
더보기
<!DOCTYPE html>
<html>
{% load static %}
<head>
<title>Blog</title>
<link href="{% static 'blog/bootstrap/bootstrap.min.css' %}" rel="stylesheet" type="text/css" media="screen">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="./index.html">Do It Django</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="./index.html">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="./blog_list.html">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="./about_me.html">About Me</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#" data-toggle="modal" data-target="#loginModal">Log In</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Modal -->
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="logInModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="loginModalLabel"> <i class="fas fa-sign-in-alt"></i>  Log In</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-6">
<button type="button" class="btn btn-outline-dark btn-block btn-sm "></button>
<button type="button" class="btn btn-outline-dark btn-block btn-sm "></button>
</div>
<div class="col-md-6">
<button type="button" class="btn btn-outline-dark btn-block btn-sm"></button>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div class="container my-3">
<div class="row">
<div class="col-md-8 col-lg-9">
<h1>Blog</h1>
<!-- Post -->
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title"></h2>
<p class="card-text"></p>
<a href="#" class="btn btn-primary">More</a>
</div>
<div class="card-footer text-muted">
Posted on January 1, 2017 by
<a href="#">Start Bootstrap</a>
</div>
</div>
<!-- Pagination -->
<ul class="pagination justify-content-center mb-4">
<li class="page-item">
<a class="page-link" href="#">← Older</a>
</li>
<li class="page-item disabled">
<a class="page-link" href="#">Newer →</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer class="py-5 bg-dark">
<div class="container"></div>
</footer>
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
crossorigin="anonymous"></script>
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
</body>
</html>
반응형
'dev-log > python' 카테고리의 다른 글
[Django] Template 문법 (0) | 2023.10.18 |
---|---|
[Django] 이미지 첨부하기 (0) | 2023.10.16 |
[django] 프로젝트 만들기 요약 (0) | 2023.10.11 |
[Python] Tkinter event 종류 (0) | 2023.02.18 |
[Python] Tkinter 이벤트 (0) | 2023.02.18 |
Comments