From dcfb9071d68c93d73df0df306794c76771c6635f Mon Sep 17 00:00:00 2001 From: Anna Date: Thu, 9 Nov 2017 17:34:42 +0300 Subject: [PATCH] upd --- blog/static/css/blog.css | 86 +++++-------------------- blog/static/css/new.css | 11 ++-- blog/templates/blog/base.html | 52 +++++++++++---- blog/templates/blog/base.html.save | 70 ++++++++++++++++++++ blog/templates/blog/post_detail.html | 42 ++++++------ blog/templates/blog/post_list.html | 10 ++- blog/templates/blog/tag_listing.html | 19 ------ blog/templatetags/__init__.py | 0 blog/templatetags/pagination_filters.py | 16 +++++ blog/urls.py | 6 +- blog/views.py | 42 ++++++------ blog/views.py.save | 71 ++++++++++++++++++++ 12 files changed, 276 insertions(+), 149 deletions(-) create mode 100644 blog/templates/blog/base.html.save delete mode 100644 blog/templates/blog/tag_listing.html create mode 100644 blog/templatetags/__init__.py create mode 100644 blog/templatetags/pagination_filters.py create mode 100644 blog/views.py.save diff --git a/blog/static/css/blog.css b/blog/static/css/blog.css index ec764a6..8c34b8d 100644 --- a/blog/static/css/blog.css +++ b/blog/static/css/blog.css @@ -1,32 +1,16 @@ -.col {padding: 2em} - -button[name="action"] {display: block; float: right; margin: 1px} -a {color: #2980b9;} - -.block, .page-header { - display: inline-block; - width: 90%; - min-height: 100px; - margin: 5px; border-radius: 5px; box-shadow: 1px 1px 3px #999999; padding: 10px; padding-left: 8%; -} - +//button[name="action"] {display: block; float: right; margin: 1px;} .close { -background: #606061; color: #FFFFFF; -line-height: 25px; position: absolute; -right: 12px; -text-align: center; -top: -10px; +right: -25px; +text-decoration: none; +top: -25px; +font-size: 30px; } -.menu_top {list-style: none; text-decoration: none;} .menu_top li {display: inline; margin-right: 10px;} -.paginator {font-size: 25px; text-align: center; } +.paginator {font-size: 20px; list-style: none; text-align: center; } .paginator a {display: inline-block; padding: 2 1; text-decoration:none; text-align: center; } - -.page-header {height: 120px;} - textarea{ width: 90%; margin:5px 10px; @@ -34,46 +18,6 @@ textarea{ .right { float: right; - text-align: end; -} - -.menu { - margin: 0; - padding: 0; - position: absolute; - display: table; - list-style: none; - display: table; - z-index: 2; -} - -.menu ul {display: none;} - -.menu li:hover ul { - display: block; - background: white; - box-shadow: 1px 1px 3px #999999; -} - -.menu li { - display: table-cell; - text-align: center; - direction: ltr; -} -.menu ul li { - display: block; - text-align: left; -} -.menu a { - display: block; - padding: 8px 14px; -} - -.menu, .sub-menu {padding: 0; margin: 0; white-space: nowrap;} - -.menu li:hover > a { - background: rgba(41, 128, 185, 0.7); - color: #fff; } .dialog { @@ -119,17 +63,19 @@ pointer-events: auto; .dialog > div { width: 17em; position: relative; -margin: 10% auto; -padding: 5px 20px 13px 20px; -border-radius: 10px; +margin: 15% auto; +padding: 10px; +text-align: center; +//border-radius: 10px; background: #fff; } +.dialog span {line-height: 50px} +.dialog input {width: 100px; margin: 5px} +//table { +//border: none; +//border-collapse: collapse;} -table { -border: none; -border-collapse: collapse;} - -td {padding: 2 10;} +//td {padding: 2 10;} tr:hover td { background-color: rgba(41, 128, 185, 0.1); diff --git a/blog/static/css/new.css b/blog/static/css/new.css index a28b0ec..23abe6e 100644 --- a/blog/static/css/new.css +++ b/blog/static/css/new.css @@ -23,13 +23,10 @@ a { letter-spacing: 1px; margin-right: 10px; } - .delete { - right: 80px; - } - .edit { - position: absolute; - right: 10px; - bottom: 10px; + .delete, .edit { + float: right; + font-size: 25px; + margin-right: 10px; } input#button-search { background: url('https://touchit.com.ua/static/images/icons/search-25.png') no-repeat center center; diff --git a/blog/templates/blog/base.html b/blog/templates/blog/base.html index a7a9e3e..6005fca 100644 --- a/blog/templates/blog/base.html +++ b/blog/templates/blog/base.html @@ -1,5 +1,5 @@ {% load staticfiles %} - +{% load pagination_filters %} BLOG @@ -20,39 +20,69 @@
+
+ {% block content %} {% endblock %} +

Последние

-
+
+

Теги

+ -
+
+
+ + + diff --git a/blog/templates/blog/base.html.save b/blog/templates/blog/base.html.save new file mode 100644 index 0000000..f8015d3 --- /dev/null +++ b/blog/templates/blog/base.html.save @@ -0,0 +1,70 @@ +{% load staticfiles %} + + + + BLOG + + + + + + + + +
+
+ {% block content %} + {% endblock %} +
+
+
+

Меню

+ +
+
+

Последние

+
    + {% for i in latest %} +
  • {{ i.title }}
    +
    {{ i.published_date }}
    + {% endfor %} +
+
+
+

Теги

+ +
+ +
+
+ + diff --git a/blog/templates/blog/post_detail.html b/blog/templates/blog/post_detail.html index af637ee..b183187 100644 --- a/blog/templates/blog/post_detail.html +++ b/blog/templates/blog/post_detail.html @@ -1,30 +1,32 @@ {% extends 'blog/base.html' %} {% block content %} -
+
+ +

{{ post.title }}

-

Теги: - {% if tags %} - {% for tag in tags %} - {{ tag }} - {% endfor %} - {% endif %} -

-

{{ post.text|safe|linebreaksbr }}

- - {% if post.published_date %} - {{ post.published_date }} - {% endif %} - -edit post -delete + {% if post.published_date %} +
{{ post.published_date}}
+ {% endif %} + {% if post.tags.names %} +
+ + {% for tag in post.tags.names %} + {{ tag }} + {% endfor %} +
+ {% endif %} +

{{ post.text|safe|linebreaksbr }}

+
-

-
{% csrf_token %} - confirm deletion of {{ post.pk }} {{ post.title }} - + x + Удалить пост "{{ post.title }}"? + {% csrf_token %} + + +
diff --git a/blog/templates/blog/post_list.html b/blog/templates/blog/post_list.html index 32628f1..cb8a4fc 100644 --- a/blog/templates/blog/post_list.html +++ b/blog/templates/blog/post_list.html @@ -2,7 +2,7 @@ {% load pagination_tags %} {% block content %} -
+ {% if posts %} {% for post in posts %}
@@ -12,7 +12,7 @@
{% for tag in post.tags.names %} - {{ tag }} + {{ tag }} {% endfor %}
{% endif %} @@ -20,5 +20,11 @@ {{ post.text|safe|linebreaksbr|truncatewords:80 }}
{% endfor %} + +{% else %} +
+

Ничего не найдено

+{% endif %} + {% endblock content %} diff --git a/blog/templates/blog/tag_listing.html b/blog/templates/blog/tag_listing.html deleted file mode 100644 index cc0cbfd..0000000 --- a/blog/templates/blog/tag_listing.html +++ /dev/null @@ -1,19 +0,0 @@ -{% extends 'blog/base.html' %} -{% load pagination_tags %} - -{% block content %} -{% for post in tagged_posts %} -
-

{{ post.title }}

- {{ post.text|safe|linebreaksbr|truncatewords:80 }}
-

Tags: - {% for tag in post.tags.names %} - {{ tag }} - {% endfor %} -

-

- {{ post.published_date}} -

-
-{% endfor %} -{% endblock content %} diff --git a/blog/templatetags/__init__.py b/blog/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/templatetags/pagination_filters.py b/blog/templatetags/pagination_filters.py new file mode 100644 index 0000000..df2e4be --- /dev/null +++ b/blog/templatetags/pagination_filters.py @@ -0,0 +1,16 @@ +from django import template + +register = template.Library() + +def truncate(paginator, number): + print paginator + print number + if number < 5: + truncated_paginator = xrange(1, number+3) + elif number > 4 and number < (paginator-3): + truncated_paginator = xrange(number-3, number+3) + else: + truncated_paginator = xrange(number-3, paginator) + return truncated_paginator + +register.filter('truncate', truncate) diff --git a/blog/urls.py b/blog/urls.py index 525156a..720c08b 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -3,10 +3,12 @@ from . import views urlpatterns = [ url(r'^$', views.post_list, name='post_list'), - url(r'^(?P[0-9]+)/$', views.post_list, name='post_list'), + #url(r'^(?P[0-9]+)/$', views.post_list, name='post_list'), + url(r'^tag/(?P[a-zA-Z]+)/$', views.post_list, name='post_list'), + #url(r'^(?P[a-zA-Z]+)/(?P[0-9]+)/$', views.post_list, name='post_list'), url(r'^post/(?P[0-9]+)/$', views.post_detail, name='post_detail'), url(r'^new/$', views.post_new, name='post_new'), url(r'^post/(?P[0-9]+)/edit/$', views.post_edit, name='post_edit'), - url(r'^tag/(?P[a-zA-Z]+)/$', views.search_by_tag, name='search_by_tag'), + #url(r'^tag/(?P[a-zA-Z]+)/$', views.search_by_tag, name='search_by_tag'), ] diff --git a/blog/views.py b/blog/views.py index 7a17bd8..74e0dbc 100644 --- a/blog/views.py +++ b/blog/views.py @@ -1,43 +1,48 @@ # -*- coding: utf-8 -*- from django.shortcuts import render from .models import Post +from django.db.models import Count, Q from django.utils import timezone from django.shortcuts import render, get_object_or_404, render_to_response from django.shortcuts import redirect from .forms import PostForm from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.contrib.auth.decorators import login_required +from taggit.models import Tag -def post_list(request, page='1'): - post_list = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')#[int(page)*5-5:int(page)*5] +def post_list(request, page='1', tag=None): + if tag is None: + post_list = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')#[int(page)*5-5:int(page)*5] + else: + post_list = Post.objects.filter(tags__name=tag).order_by('-published_date') + if request.GET.get('search'): + post_list = Post.objects.filter(Q(title__contains = request.GET.get('search')) | Q(text__contains = request.GET.get('search'))).order_by('-published_date') paginator = Paginator(post_list, 5) page = request.GET.get('page') + latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9] + tags = Tag.objects.all().annotate(c = Count('post')) try: posts = paginator.page(page) - - except PageNotAnInteger: - # If page is not an integer, deliver first 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. + except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. posts = paginator.page(paginator.num_pages) - return render_to_response('blog/post_list.html', {'posts': posts}) - -def search_by_tag(request, tag): - tagged_posts = Post.objects.filter(tags__name=tag).order_by('-published_date') + print dir(posts.paginator) + return render_to_response('blog/post_list.html', {'posts': posts, 'latest': latest, 'tags': tags}) - return render(request, 'blog/tag_listing.html', {'tagged_posts': tagged_posts}) def post_detail(request, pk): post = get_object_or_404(Post, pk=pk) tags = post.tags.names() + latest = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')[0:9] if request.POST.get('action') == 'delete': - post.delete() - print '------------delete---------------' + pk - return redirect('blog.views.post_list', 1) - - return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags}) + if request.user.is_authenticated(): + post.delete() + return redirect('/') + + else: + return redirect('/admin/login/?next=/blog/') + return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags, 'latest': latest}) @login_required(login_url="/admin/login/?next=/blog/") def post_new(request): @@ -54,6 +59,7 @@ def post_new(request): form = PostForm() return render(request, 'blog/post_edit.html', {'form': form}) + @login_required(login_url="/admin/login/?next=/blog/") def post_edit(request, pk): post = get_object_or_404(Post, pk=pk) diff --git a/blog/views.py.save b/blog/views.py.save new file mode 100644 index 0000000..dceca41 --- /dev/null +++ b/blog/views.py.save @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +from django.shortcuts import render +from .models import Post +from django.utils import timezone +from django.shortcuts import render, get_object_or_404, render_to_response +from django.shortcuts import redirect +from .forms import PostForm +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger +from django.contrib.auth.decorators import login_required + + +def post_list(request, page='1'): + post_list = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')#[int(page)*5-5:int(page)*5] + paginator = Paginator(post_list, 5) + 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_to_response('blog/post_list.html', {'posts': posts}) + + +def search_by_tag(request, tag): + tagged_posts = Post.objects.filter(tags__name=tag).order_by('-published_date') + return render(request, 'blog/tag_listing.html', {'tagged_posts': tagged_posts}) + + +def post_detail(request, pk): + post = get_object_or_404(Post, pk=pk) + tags = post.tags.names() + if request.POST.get('action') == 'delete': + print request.user.is_authenticated() + + post.delete() + return redirect('blog.views.post_list', 1) + + return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags}) + +@login_required(login_url="/admin/login/?next=/blog/") +def post_new(request): + if request.method == "POST": + form = PostForm(request.POST) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user + post.published_date = timezone.now() + post.save() + form.save_m2m() + return redirect('blog.views.post_detail', pk=post.pk) + else: + form = PostForm() + return render(request, 'blog/post_edit.html', {'form': form}) + + +@login_required(login_url="/admin/login/?next=/blog/") +def post_edit(request, pk): + post = get_object_or_404(Post, pk=pk) + if request.method == "POST": + form = PostForm(request.POST, instance = post) + if form.is_valid(): + post = form.save(commit=False) + post.author = request.user + #post.published_date = timezone.now() + post.save() + form.save_m2m() + return redirect('blog.views.post_detail', pk=post.pk) + else: + form = PostForm(instance=post) + return render(request, 'blog/post_edit.html', {'form': form})