hyeonga_code

파이선 웹구축_장고_34_투표 좋아요, 좋아요 취소 기능, 누적 조회 본문

Python_Django

파이선 웹구축_장고_34_투표 좋아요, 좋아요 취소 기능, 누적 조회

hyeonga 2023. 6. 26. 05:59
반응형

- 좋아요 기능 구현

1. 좋아요 버튼 생성 <templates> > <vote> > 'detail.html'

'detail.html'

=====

1
<a href="{% url 'board:likey' %}" class="btn btn-primary">좋아요</a>
cs

2. 경로 설정 <vote> > 'urls.py'

'urls.py'

=====

1
urlpatterns = [ path('likey/<bpk>', views.likey, name='likey') ]
cs

3. 좋아요 함수 설정 <vote> > 'views.py'

'views.py'

=====

1
2
3
4
def likey(request, bpk):
    b = Board.objects.get(id=bpk)
    b.likey.add(request.user)
    return redirect('board:detail', bpk)
cs

= 좋아요 취소 기능

4. 좋아요 누른 후, 취소 버튼 생성 <templates> > <vote> > 'detail.html'

'detail.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{% extends 'base.html' %}
{% block con %}
    <h1><b>{{ b.subject }}</b></h1>
    <div class="row mt-5">
        <div class="col-sm-9">
            <textarea class="form-control" style="height:200px" disabled>{{ b.content }}</textarea>
        </div>
        <div class="col-sm-3">
            <img src="{{b.writer.getpic}}" width="100%">
            <!-- b.writer : 레코드 자체-->
            <div class="text-center mt-3">
                <h5>written by <b>{{ b.writer }}</b></h5>
            </div>
            <div class="text-end mt-3">
                recent update: <br>
                {{ b.pubdate|date:"Y년 m월 d일/ H:i" }}
            </div>
        </div>
    </div>
    <div class="text-end mt-4">
        {% if user in b.likey.all %}
            <a href="{% url 'board:unlikey' b.id %}" class="btn btn-danger">좋아요취소</a>
        {% else %}
            <a href="{% url 'board:likey' b.id %}" class="btn btn-primary">좋아요</a>
        {% endif%}
            <a href="{% url 'board:index' %}" class="btn btn-dark">글목록</a>
        {% if user == b.writer %}
            <a href="{% url 'board:update' b.id %}" class="btn btn-dark">글수정</a>
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#remove">
                글삭제
            </button>
        {% endif %}
    </div>
 
    <!-- Modal -->
    <div class="modal fade" id="remove" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel"><b>게시글 삭제 알림창</b></h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    정말 삭제하시겠습니까?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">취소</button>
                    <a href="{% url 'board:delete' b.id %}" class="btn btn-danger">삭제</a>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
cs

5. 좋아요 취소 경로 설정 <vote> > 'urls.py'

'urls.py'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
from django.urls import path
from . import views
 
app_name="board"
urlpatterns = [
    path('index/', views.index, name='index'),
    path('detail/<bpk>', views.detail, name='detail'),
    path('delete/<bpk>', views.delete, name='delete'),
    path('create', views.create, name='create'),
    path('update/<bpk>', views.update, name='update'),
    path('likey/<bpk>', views.likey, name='likey'),
    path('unlikey/<bpk>', views.unlikey, name='unlikey'),
cs

6. 함수 설정 <vote> > 'views.py'

'views.py'

=====

1
2
3
4
def unlikey(request, bpk):
    b = Board.objects.get(id=bpk)
    b.likey.remove(request.user)
    return redirect('board:detail', bpk)
cs

= 메인 페이지에서 좋아요 숫자 확인하기

7. 메인 페이지 작성 <templates> > <vote> > 'index.html'

+ <td>{{i.likey.count}}</td> 추가

'index.html'

=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{% extends 'base.html' %}
{% load mathfilters %}
{% block con %}
    <h1><b>게시판</b></h1>
        
    <div class="text-end">
        <a href="{% url 'board:create' %}" class="btn btn-dark">게시글생성</a>
    </div>
 
    <table class="table table-hover mt-4">
        <thead>
            <tr class="table-dark">
                <th scope="col">NO</th>
                <th scope="col">SUBJECT</th>
                <th scope="col">SUMMARY</th>
                <th scope="col">WRITER</th>
                <th scope="col">LIKEY</th>
            </tr>
        </thead>
        <tbody>
            {% for i in bset %}
                <tr>
                    <th scope="row">{{i.id}}</th>
                    <td><a class="sub" href="{% url 'board:detail' i.id %}">{{i.subject}}</a></td>
                    <td>{{ i.content|truncatewords:3 }}</td>
                    <!--글자 수 제한-->
                    <td>{{i.writer}}</td>
                    <td>{{i.likey.count}}</td>
                </tr>
            {% empty %}
                <tr>
                    {% if kw %}
                        <th> 첫 번째 게시글을 작성해주세요!</th>
                    {% else %}
                        <th> 첫 번째 게시글을 작성해주세요!</th>
                    {% endif %}
                </tr>
            {% endfor %}
        </tbody>
    </table>
 
    <style>
        .sub {
            text-decoration: none;
            color:black;
            font-weight: bold;
        }
        .sub:hover{
            color:red;
        }
    </style>
 
    {% if bset.has_previous %}
        <nav aria-label="Page navigation example">
                <ul class="pagination justify-content-center">
                    <li class="page-item" ><a class="page-link" href="{% url 'board:index' %}?page={{ bset.start_page }}&cate={{cate}}&kw={{kw}}">HOME</a></li>
                    {% if bset.has_previous%}
                        <li class="page-item">
                            <a href="{% url 'board:index' %}?page={{ bset.previous_page_number}}&cate={{cate}}&kw={{kw}}" class="page-link">PRE</a>
                        </li>
                    {% endif %}
                        {% for i in bset.paginator.page_range %}
                            {% if bset.number|add:3 >= i and i >= bset.number|sub:3%}
                                <li class="page-item {% if bset.number == i %}active{% endif %}"  disabled ><a class="page-link" href="{% url 'board:index' %}?page={{i}}">{{i}}</a></li>
                            {% endif %}
                        {% endfor %}
                    {% if bset.has_next%}
                        <li class="page-item">
                            <a class="page-link" href="{% url 'board:index' %}?page={{ bset.next_page_number}}&cate={{cate}}&kw={{kw}}">NEXT</a>
                        </li>
                    {% endif %}
 
                    <li class="page-item"><a class="page-link" href="{% url 'board:index' %}?page={{bset.paginator.num_pages}}&cate={{cate}}&kw={{kw}}">FIN</a></li>
                </ul>
        </nav>
    {% elif not bset.has_previous %}
        <nav aria-label="Page navigation example">
            <ul class="pagination justify-content-center">
                <li class="page-item" ><a class="page-link" href="{% url 'board:index' %}?page={{ bset.start_page }}&cate={{cate}}&kw={{kw}}">HOME</a></li>
                {% if bset.has_previous%}
                    <li class="page-item">
                        <a href="{% url 'board:index' %}?page={{ bset.previous_page_number}}&cate={{cate}}&kw={{kw}}" class="page-link">PRE</a>
                    </li>
                {% endif %}
                {% for i in bset.paginator.page_range %}
                    {% if bset.number|add:3 >= i and i >= bset.number|sub:3%}
                        <li class="page-item {% if bset.number == i %}active{% endif %}"  disabled ><a class="page-link" href="{% url 'board:index' %}?page={{i}}">{{i}}</a></li>
                    {% endif %}
                {% endfor %}
                {% if bset.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{% url 'board:index' %}?page={{bset.next_page_number}}&cate={{cate}}&kw={{kw}}">NEXT</a>
                    </li>
                {% elif not bset.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{% url 'board:index' %}?page={{bset.next_page_number}}&cate={{cate}}&kw={{kw}}" disabled>Next</a>
                    </li>
                {% endif %}
                <li class="page-item"><a class="page-link" href="{% url 'board:index' %}?page={{bset.paginator.num_pages}}&cate={{cate}}&kw={{kw}}">마지막</a></li>
            </ul>
        </nav>
        {{paginator.num_pages}}
    {% endif %}
 
    <div class ='row'>
        <div class='col-sm-2' name='cate'>
            <select calss ='form-select' name='cate' style ='width:100%; height:100%'>
                <option value="sub" {% if cate == 'sub' %} selected {% endif %}>제목</option>
                <option value='wri' {% if cate == 'wri' %} selected {% endif %}>작성자</option>
                <option value="con" {% if cate == 'con' %} selected {% endif %}>내용</option>
                <!--검색 데이터 유지-->
            </select>
        </div>
        <div class='col-sm-6'>
            <input type='text' class='form-control' name='kw' value="{{kw}}" >
        </div>
        <div class='col-sm-2'>
            <button class='btn btn-secondary' style ='width:100%'>Search</button>
        </div>
        <div class='col-sm-2'>
            <a href="{% url 'board:index' %}"><button type="button" class='btn btn-dark' style ='width:100%'>Reset</button></a>
        </div>
        <div class='col-sm-2'></div>
        <div class='col-sm-2'></div>
    </div>
{% endblock %}
cs

 

 

반응형