pagination

This commit is contained in:
Anna Sudnitsina 2018-03-28 11:44:05 +03:00
parent c30afc1d95
commit 446842d2d4
7 changed files with 48 additions and 26 deletions

View File

@ -27,7 +27,7 @@ SECRET_KEY = '1a+3jmf#g!q_gj66$^v9_&8uoev=%k49=j(%lp%!kvv$6x3bn^'
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
ALLOWED_HOSTS = ["imgcollection.herokuapp.com", "byte.difrex.ru"] ALLOWED_HOSTS = ["imgcollection.herokuapp.com", "byte.difrex.ru", "pythonanywhere.com"]
# Application definition # Application definition

View File

@ -32,7 +32,7 @@
text-decoration: none; text-decoration: none;
} }
li { li {
list-style-type: none list-style-type: none
} }
@ -53,7 +53,7 @@
text-decoration: none; text-decoration: none;
} }
.menu a:hover { .menu a:hover, .paginator a:hover {
color:#181D2B; color:#181D2B;
} }
@ -92,6 +92,16 @@
background: url('https://raw.githubusercontent.com/niklausgerber/PreLoadMe/master/img/status.gif') no-repeat 50% 50%; background: url('https://raw.githubusercontent.com/niklausgerber/PreLoadMe/master/img/status.gif') no-repeat 50% 50%;
margin: -16px 0 0 -16px; margin: -16px 0 0 -16px;
} }
.paginator a {
color: #4368AD;
font-family: 'Open Sans', sans-serif;
font-size: 20px;
display: inline-block;
padding: 25px;
/*background: #e0e0e0;*/
width: 90%;
text-decoration: none;
}
@media screen and (max-width: 450px) { @media screen and (max-width: 450px) {

View File

@ -0,0 +1,4 @@
$('.menu-icon').click(function() {if ($('.menu').css('display') == 'block') {
$('.menu').css('display', 'none') } else {
$('.menu').css('display', 'block')
}})

View File

@ -3,11 +3,19 @@
{% load static %} {% load static %}
{% block content%} {% block content%}
<div id="page-preloader"><span class="spinner"></span></div>
<div class="images"> <div class="images">
{% for image in all %}<a href="{% url 'image_detail' pk=image.pk %}" ><img src= "/media/images{{image.thumb_name }}"></a>{% endfor %} {% for image in all %}<a href="{% url 'image_detail' pk=image.pk %}" ><img src= "/media/images{{image.thumb_name }}"></a>{% endfor %}
</div> </div>
<div class="paginator">
{% if all.has_previous %}
<a href="?page={{ all.previous_page_number }}">Previous</a> <nobr>
{% endif %}
{% if all.has_next %}
<a href="?page={{ all.next_page_number }}"> Next</a>
{% endif %}
</div>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script> <script type="text/javascript" src="{% static 'imagehosting/js/script.js' %}"></script>
</script>
{% endblock content %} {% endblock content %}

View File

@ -9,12 +9,8 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="{% static 'imagehosting/css/style.css' %}"> <link rel="stylesheet" href="{% static 'imagehosting/css/style.css' %}">
<script type="text/javascript" src="{% static 'imagehosting/js/script.js' %}"></script>
<style>
</style>
</head> </head>
<body> <body>
<div id="page-preloader"><span class="spinner"></span></div>
<div class='header'> <a href="{% url 'all_posts' %}" class='logo'>LOGO </a> <div class='header'> <a href="{% url 'all_posts' %}" class='logo'>LOGO </a>
<a href="{% url 'image_new' %}" class='right-menu'>new image</a> <a href="{% url 'image_new' %}" class='right-menu'>new image</a>
<a href="{% url 'all_posts' %}" class='right-menu'>all images</a> <a href="{% url 'all_posts' %}" class='right-menu'>all images</a>
@ -30,10 +26,5 @@
{% block content %} {% block content %}
{% endblock%} {% endblock%}
</body> </body>
<script> <script type="text/javascript" src="{% static 'imagehosting/js/menu.js' %}"></script>
$('.menu-icon').click(function() {if ($('.menu').css('display') == 'block') {
$('.menu').css('display', 'none') } else {
$('.menu').css('display', 'block')
}})
</script>
</html> </html>

View File

@ -1,4 +1,4 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.conf.urls import url from django.conf.urls import url
from . import views from . import views
@ -10,9 +10,9 @@ from django.contrib.auth.views import login, logout
urlpatterns = [ urlpatterns = [
url(r'^$', views.image_new, name='image_new'), url(r'^new$', views.image_new, name='image_new'),
url(r'^post/(?P<pk>[0-9]+)/$', views.image_detail, name='image_detail'), url(r'^post/(?P<pk>[0-9]+)/$', views.image_detail, name='image_detail'),
url(r'^all/$', views.all_posts, name='all_posts'), url(r'^$', views.all_posts, name='all_posts'),
url(r'^delete/(?P<pk>[0-9]+)/$', views.delete_post, name='delete_post'), url(r'^delete/(?P<pk>[0-9]+)/$', views.delete_post, name='delete_post'),
url(r'^admin/', admin.site.urls), url(r'^admin/', admin.site.urls),
url(r'^login$', login, {'template_name':'imagehosting/login.html'}, name='login'), url(r'^login$', login, {'template_name':'imagehosting/login.html'}, name='login'),

View File

@ -8,7 +8,7 @@ from ih import files, images
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from ih import images from ih import images
from django.views.decorators.csrf import requires_csrf_token from django.views.decorators.csrf import requires_csrf_token
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def create_thumb_from_file(filename): def create_thumb_from_file(filename):
orig_file = 'imagehost/media/images/' + filename orig_file = 'imagehost/media/images/' + filename
@ -21,7 +21,7 @@ def create_thumb_from_file(filename):
) )
return thumb_name return thumb_name
@login_required(login_url="/login") @login_required(login_url="/login")
def image_new(request): def image_new(request):
if request.method == "POST": if request.method == "POST":
@ -35,15 +35,24 @@ def image_new(request):
form = PostForm() form = PostForm()
return render(request, 'imagehosting/index.html', {'form': form}) return render(request, 'imagehosting/index.html', {'form': form})
def image_detail(request, pk): def image_detail(request, pk):
post = get_object_or_404(Post, pk=pk) post = get_object_or_404(Post, pk=pk)
return render(request, 'imagehosting/image.html', {'post': post}) return render(request, 'imagehosting/image.html', {'post': post})
def all_posts(request): def all_posts(request, page='1'):
all_posts = Post.objects.order_by('-pk') img_list = Post.objects.order_by('-pk')
return render(request, 'imagehosting/all.html', {'all': all_posts}) paginator = Paginator(img_list, 50)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger: # If page is not an integer, deliver first page.
posts = paginator.page(1)
except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results.
posts = paginator.page(paginator.num_pages)
return render(request, 'imagehosting/all.html', {'all': posts})
@login_required(login_url="/login") @login_required(login_url="/login")