From 446842d2d4093c07a48147b35b6a1f136fdd175c Mon Sep 17 00:00:00 2001 From: Anna Date: Wed, 28 Mar 2018 11:44:05 +0300 Subject: [PATCH] pagination --- imagehost/settings.py | 2 +- .../static/imagehosting/css/style.css | 14 +++++++++-- imagehosting/static/imagehosting/js/menu.js | 4 ++++ imagehosting/templates/imagehosting/all.html | 14 ++++++++--- imagehosting/templates/imagehosting/base.html | 11 +-------- imagehosting/urls.py | 6 ++--- imagehosting/views.py | 23 +++++++++++++------ 7 files changed, 48 insertions(+), 26 deletions(-) create mode 100644 imagehosting/static/imagehosting/js/menu.js diff --git a/imagehost/settings.py b/imagehost/settings.py index e4bc88f..c4f71a4 100644 --- a/imagehost/settings.py +++ b/imagehost/settings.py @@ -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! DEBUG = True -ALLOWED_HOSTS = ["imgcollection.herokuapp.com", "byte.difrex.ru"] +ALLOWED_HOSTS = ["imgcollection.herokuapp.com", "byte.difrex.ru", "pythonanywhere.com"] # Application definition diff --git a/imagehosting/static/imagehosting/css/style.css b/imagehosting/static/imagehosting/css/style.css index 452da03..712b57e 100644 --- a/imagehosting/static/imagehosting/css/style.css +++ b/imagehosting/static/imagehosting/css/style.css @@ -32,7 +32,7 @@ text-decoration: none; } - li { + li { list-style-type: none } @@ -53,7 +53,7 @@ text-decoration: none; } - .menu a:hover { + .menu a:hover, .paginator a:hover { color:#181D2B; } @@ -92,6 +92,16 @@ background: url('https://raw.githubusercontent.com/niklausgerber/PreLoadMe/master/img/status.gif') no-repeat 50% 50%; 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) { diff --git a/imagehosting/static/imagehosting/js/menu.js b/imagehosting/static/imagehosting/js/menu.js new file mode 100644 index 0000000..ede76a4 --- /dev/null +++ b/imagehosting/static/imagehosting/js/menu.js @@ -0,0 +1,4 @@ +$('.menu-icon').click(function() {if ($('.menu').css('display') == 'block') { + $('.menu').css('display', 'none') } else { + $('.menu').css('display', 'block') +}}) diff --git a/imagehosting/templates/imagehosting/all.html b/imagehosting/templates/imagehosting/all.html index 12b37f5..1da5660 100644 --- a/imagehosting/templates/imagehosting/all.html +++ b/imagehosting/templates/imagehosting/all.html @@ -3,11 +3,19 @@ {% load static %} {% block content%} +
{% for image in all %}{% endfor %}
- +
+ {% if all.has_previous %} + Previous + {% endif %} + {% if all.has_next %} + Next + {% endif %} +
- + + {% endblock content %} diff --git a/imagehosting/templates/imagehosting/base.html b/imagehosting/templates/imagehosting/base.html index e6c1876..aedbe5c 100644 --- a/imagehosting/templates/imagehosting/base.html +++ b/imagehosting/templates/imagehosting/base.html @@ -9,12 +9,8 @@ - - -
new image all images @@ -30,10 +26,5 @@ {% block content %} {% endblock%} - + diff --git a/imagehosting/urls.py b/imagehosting/urls.py index 71e79b7..daf0358 100644 --- a/imagehosting/urls.py +++ b/imagehosting/urls.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- from django.conf.urls import url from . import views @@ -10,9 +10,9 @@ from django.contrib.auth.views import login, logout urlpatterns = [ - url(r'^$', views.image_new, name='image_new'), + url(r'^new$', views.image_new, name='image_new'), url(r'^post/(?P[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[0-9]+)/$', views.delete_post, name='delete_post'), url(r'^admin/', admin.site.urls), url(r'^login$', login, {'template_name':'imagehosting/login.html'}, name='login'), diff --git a/imagehosting/views.py b/imagehosting/views.py index 569454e..e181a04 100644 --- a/imagehosting/views.py +++ b/imagehosting/views.py @@ -8,7 +8,7 @@ from ih import files, images from django.contrib.auth.decorators import login_required from ih import images from django.views.decorators.csrf import requires_csrf_token - +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger def create_thumb_from_file(filename): orig_file = 'imagehost/media/images/' + filename @@ -21,7 +21,7 @@ def create_thumb_from_file(filename): ) return thumb_name - + @login_required(login_url="/login") def image_new(request): if request.method == "POST": @@ -35,15 +35,24 @@ def image_new(request): form = PostForm() return render(request, 'imagehosting/index.html', {'form': form}) - + def image_detail(request, pk): post = get_object_or_404(Post, pk=pk) return render(request, 'imagehosting/image.html', {'post': post}) - -def all_posts(request): - all_posts = Post.objects.order_by('-pk') - return render(request, 'imagehosting/all.html', {'all': all_posts}) + +def all_posts(request, page='1'): + img_list = Post.objects.order_by('-pk') + 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")