Skip to content

Commit

Permalink
pagenation
Browse files Browse the repository at this point in the history
  • Loading branch information
UiHyeon committed Aug 9, 2019
1 parent eaab443 commit 9ba5475
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 78 deletions.
123 changes: 75 additions & 48 deletions board/templates/board.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,84 @@
{% block content %}


<h1 style="text-align: center;">GaSiRi Let's get it! man~</h1>
<div class="container" style="padding-top: 100px;">
<h1 style="text-align: center;">GaSiRi</h1>

<div style="padding-top: 200px;">
<table class="table table-hover">
<thead>
<tr style="text-align: center;">
<th scope="col">순번</th>
<th scope="col">글제목</th>
<th scope="col">작성자</th>
<th scope="col">작성일자</th>
</tr>
</thead>
<tbody>
{% for board in posts %}
<tr onclick="location.href='{% url 'test' board.id %}'" style="text-align: center;">
<th scope="row">{{board.id}}</th>
<td>{{board.title}}</td>
<td>{{board.userName}}</td>
<td>{{board.date}}</td>
</tr>
<div style="padding-top: 100px;">
<table class="table table-hover">
<thead>
<tr>
<th scope="col" style="text-align: center;">순번</th>
<th scope="col" style="text-align: center;">글제목</th>
<th scope="col" style="text-align: center;">작성자</th>
<th scope="col" style="text-align: center;">작성일자</th>
</tr>
</thead>
<tbody>
{% for board in posts %}
<tr onclick="location.href='{% url 'test' board.id %}'">
<th scope="row" style="text-align: center;">{{board.id}}</th>
<td style="text-align: center;">{{board.title}}</td>
<td style="text-align: center;">{{board.userName}}</td>
<td style="text-align: center;">{{board.date}}</td>
</tr>

{%endfor%}
</tbody>
</table>
</div>

<div >
<div >
{%if posts.has_previous%}
<a href="?page=1">First</a>
<a href="?page={{posts.previous_page_number}}">Previous</a>
{%endif%}

<span>{{posts.number}}</span>
<span>of</span>
<span>{{posts.paginator.num_pages}}</span>

{%if posts.has_next%}
<a href="?page={{posts.next_page_number}}">Next</a>
<a href="?page={{posts.paginator.num_pages}}">Last</a>
{%endif%}
</div>

{% if user.is_authenticated %}
<a href = "{% url 'board_new' %}"><button type="button" class="btn btn-danger" style="float: right;">글쓰기</button></a>
{% else %}
<button type="button" onclick="alert('로그인을 해주세요'); location.replace('../accounts/login/');" class="btn btn-danger" style="float: right;">글쓰기</button>
{% endif %}
{%endfor%}
</tbody>
</table>
</div>

</div>
<div >
<div >
<nav aria-label="Page navigation example">
<ul class="pagination">
{%if posts.has_previous%}
<li class="page-item">
<a class="page-link" href="?page={{posts.previous_page_number}}" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="" aria-label="Previous">
<span aria-disabled="true">&laquo;</span>
</a>
</li>

{%endif%}



{% for i in p_range %}

{% if i == page %}
<li class="page-item active"><a class="page-link" href="?page={{i}}">{{i}}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{i}}">{{i}}</a></li>
{% endif %}
{% endfor %}

{%if posts.has_next%}
<li class="page-item">
<a class="page-link" href="?page={{posts.next_page_number}}" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="" aria-label="Next">
<span aria-disabled="true">&raquo;</span>
</a>
</li>
{%endif%}
</ul>
</nav>
</div>
{% if user.is_authenticated %}
<a href = "{% url 'board_new' %}"><button type="button" class="btn btn-danger" style="float: right;">글쓰기</button></a>
{% else %}
<button type="button" onclick="alert('로그인을 해주세요'); location.replace('../accounts/login/');" class="btn btn-danger" style="float: right;">글쓰기</button>
{% endif %}
</div>

</div>
{% endblock %}
57 changes: 29 additions & 28 deletions board/templates/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,40 @@ <h2>{{ board.title }}</h2>


<a href="{% url 'board' %}">글목록</a>
</div>

<br>
<br>

<h4> 댓글</h4>


<form method="POST" action='{% url "createcomment" board.id %}'>
{% csrf_token %}
<div>
<textarea class="form-control" id="exampleFormControlTextarea1" name="text" rows="3"></textarea>
<h4>가격 : </h4> <input type="number" name="price" min="1" max="10">
</div>
<br>
<div align='right'>
<button type="submit" class="btn btn-primary">등록</button>
</div>
</form>
<br>

<hr border='1'>
{% for comments in comment.all %}
<div class="comment">
<div class="date">
<strong>{{ comments.author }}</strong>
{{ comments.created_date }}
<h4> 댓글</h4>

<form method="POST" action='{% url "createcomment" board.id %}'>
{% csrf_token %}
<div>
<textarea class="form-control" id="exampleFormControlTextarea1" name="text" rows="3"></textarea>
<h4>가격 : </h4> <input type="number" name="price" min="1" max="10">
</div>
<p>{{ comments.text|linebreaks }}</p>
<p>제시한 가격 : {{comments.price}}</p>
</div>
<br>
<div align='right'>
<button type="submit" class="btn btn-primary">등록</button>
</div>
</form>

<hr border='1'>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
{% for comments in comment.all %}
<div class="comment">
<div class="date">
<strong>{{ comments.author }}</strong>
{{ comments.created_date }}
</div>
<p>{{ comments.text|linebreaks }}</p>
<p>제시한 가격 : {{comments.price}}</p>
</div>
<hr border='1'>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
</div>


{% endblock %}
13 changes: 11 additions & 2 deletions board/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,18 @@ def board(request):
boards = Board.objects
boards_list = Board.objects.all()
paginator = Paginator(boards_list,3)
page = request.GET.get('page')
if(request.GET.get('page') == None):
page=1
else:
page = int(request.GET.get('page'))

posts = paginator.get_page(page)
return render(request, 'board.html',{'boards' : boards , 'posts':posts})
page_range = 5 #페이지 범위 지정 예 1, 2, 3, 4, 5 / 6, 7, 8, 9, 10
current_block = math.ceil(page/page_range) #해당 페이지가 몇번째 블럭인가
start_block = (current_block-1) * page_range
end_block = start_block + page_range
p_range = paginator.page_range[start_block:end_block]
return render(request, 'board.html',{'boards' : boards , 'posts':posts , 'p_range':p_range , 'page': page})

def createcomment(request, board_id):
#board = get_object_or_404(Board, pk=board_id) #게시글들에서 하나 뽑음
Expand Down

0 comments on commit 9ba5475

Please sign in to comment.