Python/Django

[Django Tutorial] 6. 장고 템플릿

  • -
728x90

웹 프레임워크 Django(장고) 공부
개발 언어 : python 3.8.1
개발 환경 : Visual studio Code
참조 : 장고걸스 튜토리얼

템플릿 동적 데이터

블로그 글은 각각 다른 장소에 조각조각 나누어져있다. Post 모델은 models.py 파일에, post_list 모델은 views.py 파일에 존재한다.

뷰(View)는 모델과 템플릿을 연결하는 역할을 한다. post_list를 뷰에서 보여주고 이를 템플릿에 전달하기 위해서는 모델을 가져와야한다. 일반적으로 템플릿 에서 모델을 선택하도록 만들어야 한다.

blog/view.pyfrom .models import Post를 추가한다.

from django.shortcuts import render
from .models import Post

# Create your views here.
def post_list(request):
    return render(request, 'blog/post_list.html', {})

from 다음에 있는 .은 현재 디렉토리(폴더) 또는 애플리케이션을 의미한다. 동일한 디렉토리 안에 veiw.py, models.py 파일이 존재하기 때문에 확장자를 붙이지 않아도 내용을 가져올 수 있다.

Post모델에서 블로그 글을 가져오기 위해서는 쿼리셋(QuerySet)이 필요하다.

쿼리셋(QuerySet)

블로그 글을 published_date기준으로 정렬해보다.

이 과정을 진행하는 도중에 python manage.py 를 실행하려니 아래와 같은 오류가 발생했다.
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: contenttypes
구글링을 해도 죄다 영문이고... 대충 해석해서 알아봐도 뭐가 뭔지 잘 모르겠고... 그래서 좀 더 자세히 읽어보았다.
문제는 settings.py 안의 INSTALLED_APPS에 'django.contrib.contenttypes' 이게 중복되어잇었다.. 이유가 뭐지..?

blog/views.py를 아래와 같이 수정한다.

from django.shortcuts import render
from django.utils import timezone
from .models import Post


# Create your views here.
def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {})

아직 posts파일이 적용되지 않았지만 나중에 posts파일로 QuerySet을 템플릿 컨텍스트에 전달하기 위해 만들어둔다.
render함수에는 매개변수 request(사용자가 요청하는 모든 것))와 blog/post_list.html 템플릿이 있다.
return render(request, 'blog/post_list.html', {'posts': posts}) 로 수정하여 템플릿을 사용하기 위한 매개변수 post를 추가한다.

from django.shortcuts import render
from django.utils import timezone
from .models import Post


# Create your views here.
def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

장고 템플릿

템플릿 태그는 정적인 HTML에 동적인 pythonHTML로 바꾸어주어 빠르고 쉽게 동적인 웹사이트로 만들 수 있게 도와준다.

post 목록 템플릿 보여주기

blog/templates/blog/post_list.html을 아래와 같이 수정해준다.

<html>
    <head>
        <title>wonillism blog</title>
    </head>
    <body>
        <div>
            <h1><a href="/">wonillism Blog</a></h1>
        </div>
        {% for post in posts %}
        <div>
            <p>published: {{ post.published_date }}</p>
            <h1><a href="">{{ post.title }}</a></h1>
            <p>{{ post.text|linebreaksbr }}</p>
        </div>
        {% endfor %}
    </body>
</html>

 

이제 슬슬 블로그의 모양이 나오는 것 같다!

728x90
300x250
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.