이번에는 태그와 검색 기능을 구현해보겠습니다.
post_list.html
에 아직 포스트의 content를 노출시킨 적이 없습니다. 포스트의 content를 노출하는 부분을 추가해줍니다. div.likes.m_text 태그 아래에 추가합니다.
파일명 : post/templates/post/post_list.html
<div class="likes m_text">
<span id="like-count-{{ post.id }}">좋아요{{ post.like_count}}개 </span>
<span id="bookmark-count-{{ post.id }}"> 북마크{{ post.bookmark_count}} 개</span>
</div>
**<div class="content">{{ post.content }}</div>**
이제 컨텐츠가 출력되는 것을 볼 수 있습니다.
인스타그램을 보시면 글 내용 중 해시태그가 있는 부분은 자동으로 링크로 표시가 되고, 클릭하면 해당 해시태그와 관련된 게시글이 검색되는 시스템입니다. 지금은 검색창과 태그 기능이 구현 되어있지 않습니다. 글의 content에 있는 내용 중에 정규표현식을 이용해 해시태그를 분리하도록 하겠습니다. 링크를 주고 태그 모델에다가 저장하는 것까지 해보도록 하겠습니다.
post/models.py
에 Tag 모델을 추가합니다.
파일명 : post/models.py
class Tag(models.Model):
name = models.CharField(max_length=140, unique=True)
def __str__(self):
return self.name