This commit is contained in:
Anna Sudnitsina 2017-11-09 17:34:42 +03:00
parent 08eee575b2
commit dcfb9071d6
12 changed files with 276 additions and 149 deletions

View File

@ -1,32 +1,16 @@
.col {padding: 2em} //button[name="action"] {display: block; float: right; margin: 1px;}
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%;
}
.close { .close {
background: #606061;
color: #FFFFFF; color: #FFFFFF;
line-height: 25px;
position: absolute; position: absolute;
right: 12px; right: -25px;
text-align: center; text-decoration: none;
top: -10px; top: -25px;
font-size: 30px;
} }
.menu_top {list-style: none; text-decoration: none;}
.menu_top li {display: inline; margin-right: 10px;} .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; } .paginator a {display: inline-block; padding: 2 1; text-decoration:none; text-align: center; }
.page-header {height: 120px;}
textarea{ textarea{
width: 90%; width: 90%;
margin:5px 10px; margin:5px 10px;
@ -34,46 +18,6 @@ textarea{
.right { .right {
float: 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 { .dialog {
@ -119,17 +63,19 @@ pointer-events: auto;
.dialog > div { .dialog > div {
width: 17em; width: 17em;
position: relative; position: relative;
margin: 10% auto; margin: 15% auto;
padding: 5px 20px 13px 20px; padding: 10px;
border-radius: 10px; text-align: center;
//border-radius: 10px;
background: #fff; background: #fff;
} }
.dialog span {line-height: 50px}
.dialog input {width: 100px; margin: 5px}
//table {
//border: none;
//border-collapse: collapse;}
table { //td {padding: 2 10;}
border: none;
border-collapse: collapse;}
td {padding: 2 10;}
tr:hover td { tr:hover td {
background-color: rgba(41, 128, 185, 0.1); background-color: rgba(41, 128, 185, 0.1);

View File

@ -23,13 +23,10 @@ a {
letter-spacing: 1px; letter-spacing: 1px;
margin-right: 10px; margin-right: 10px;
} }
.delete { .delete, .edit {
right: 80px; float: right;
} font-size: 25px;
.edit { margin-right: 10px;
position: absolute;
right: 10px;
bottom: 10px;
} }
input#button-search { input#button-search {
background: url('https://touchit.com.ua/static/images/icons/search-25.png') no-repeat center center; background: url('https://touchit.com.ua/static/images/icons/search-25.png') no-repeat center center;

View File

@ -1,5 +1,5 @@
{% load staticfiles %} {% load staticfiles %}
{% load pagination_filters %}
<html> <html>
<head> <head>
<title>BLOG</title> <title>BLOG</title>
@ -20,39 +20,69 @@
</div> </div>
</div> </div>
<div id="content"> <div id="content">
<div id="posts">
{% block content %} {% block content %}
{% endblock %} {% endblock %}
</div>
<div class="sidemenu"> <div class="sidemenu">
<div class="block"> <div class="block">
<h2>Меню</h2> <h2>Меню</h2>
<ul> <ul>
<li><li><a href="{% url 'post_new' %}">new post</a></li> <li><li><a href="{% url 'post_new' %}">new post</a></li>
<li><a href="/blog/1">all notes</a></li> <li><a href="/">all notes</a></li>
</ul> </ul>
</div> </div>
<div class="block"> <div class="block">
<h2>Последние</h2> <h2>Последние</h2>
<ul> <ul>
<li><a href="/">ToDo List</a><div class="date">2017-11-04</div></li> {% for i in latest %}
<li><a href="/">Йа сцылко</a><div class="date">2017-11-03</div></li> <li><a href="{% url 'post_detail' pk=i.pk %}"> {{ i.title }} </a> <br>
<li><a href="/">Йа сцылко №2</a><div class="date">2017-11-03</div></li> <div class="date"> {{ i.published_date }} </div>
{% endfor %}
</ul>
</div>
<div class="block">
<h2> Теги </h2>
<ul>
{% for tag in tags %}
<li><li><a href="{% url 'post_list' tag=tag %}">{{ tag.name }} : {{ tag.c }}</a></li>
{% endfor %}
</ul> </ul>
</div> </div>
</div> </div>
</div> </div>
<div id="pagination">
<p>
{% if posts.has_previous %} <a href="?page={{ posts.previous_page_number }}">предыдущая</a>{% endif %}
{% if posts.has_next %}<a href="?page={{ posts.next_page_number }}">следующая</a> {% endif %}
</p>
{% if posts.has_other_pages %}
Страницы:
<a href="?page=1">|<<</a>
{% for page in posts.paginator.num_pages|truncate:posts.number %}
{% if page == posts.number %} {{page}}
{% else %} <a href="?page={{page}}">{{page}}</a> {% endif %} {% endfor %}
<a href="?page={{ posts.paginator.num_pages }}"> >>| </a>
{% endif %}
<ul class="menu_top paginator"> <ul class="menu_top paginator">
{% if posts.has_previous %} {% if posts.has_previous %}
<li> <a href="?page=1"><<</a> </li> <li> <a href="?page=1"><<</a> </li>
<li> <a href="?page={{ posts.previous_page_number }}">{{ posts.previous_page_number }}</a> </li> <li> <a href="?page={{ posts.previous_page_number }}">{{ posts.previous_page_number }}</a> </li>
{% endif %} {% endif %}
<li> <a style="color: #000000" href="">{{ posts.number }}<a></li> {% if posts.has_other_pages %}<li> <a style="color: #000000" href="">{{ posts.number }}</a></li> {% endif %}
{% if posts.has_next %} {% if posts.has_next %}
<li> <a href="?page={{ posts.next_page_number }}"> {{ posts.next_page_number }}</a> </li> <li> <a href="?page={{ posts.next_page_number }}"> {{ posts.next_page_number }}</a> </li>
<li> <a href="?page={{ posts.next_page_number|add:1 }}">{{ posts.next_page_number|add:1 }}</a></li> <!--li> <a href="?page={{ posts.next_page_number|add:1 }}">{{ posts.next_page_number|add:1 }}</a></li-->
<li><a href="?page={{ posts.paginator.num_pages }}"> >> </a></li> {% endif %} <li> <a href="?page={{ posts.paginator.num_pages }}"> >> </a></li> {% endif %}
</body>
{% comment %} {% comment %}
<!--li> {% for i in posts.paginator.page_range %} </li--> <!--li> {% for i in posts.paginator.page_range %} </li-->
{% endcomment %} {% endcomment %}
</ul> </ul>
</div>
</body>
</html> </html>

View File

@ -0,0 +1,70 @@
{% load staticfiles %}
<html>
<head>
<title>BLOG</title>
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
<link rel="stylesheet" href="{% static 'css/new.css' %}">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css">
</head>
<body>
<div id="header">
<a id="logo" href="/">Django Blog</a>
<div id="search">
<form id="search-form">
<input name="search" type="text"></input>
<input id="button-search" type="submit" value=""></input>
</form>
</div>
</div>
<div id="content">
<div id="posts">
{% block content %}
{% endblock %}
</div>
<div class="sidemenu">
<div class="block">
<h2>Меню</h2>
<ul>
<li><li><a href="{% url 'post_new' %}">new post</a></li>
<li><a href="/">all notes</a></li>
</ul>
</div>
<div class="block">
<h2>Последние</h2>
<ul>
{% for i in latest %}
<li><a href="{% url 'post_detail' pk=i.pk %}"> {{ i.title }} </a> <br>
<div class="date"> {{ i.published_date }} </div>
{% endfor %}
</ul>
</div>
<div class="block">
<h2>Теги</h2>
<ul>
<li><li><a href="{% url 'post_new' %}">new post</a></li>
<li><a href="/">all notes</a></li>
</ul>
</div>
</div>
</div>
<ul class="menu_top paginator">
{% if posts.has_previous %}
<li> <a href="?page=1"><<</a> </li>
<li> <a href="?page={{ posts.previous_page_number }}">{{ posts.previous_page_number }}</a> </li>
{% endif %}
<li> <a style="color: #000000" href="">{{ posts.number }}<a></li>
{% if posts.has_next %}
<li> <a href="?page={{ posts.next_page_number }}"> {{ posts.next_page_number }}</a> </li>
<li> <a href="?page={{ posts.next_page_number|add:1 }}">{{ posts.next_page_number|add:1 }}</a></li>
<li><a href="?page={{ posts.paginator.num_pages }}"> >> </a></li> {% endif %}
</body>
{% comment %}
<!--li> {% for i in posts.paginator.page_range %} </li-->
{% endcomment %}
</ul>
</html>

View File

@ -1,30 +1,32 @@
{% extends 'blog/base.html' %} {% extends 'blog/base.html' %}
{% block content %} {% block content %}
<div class="post" style="position: relative; "> <div class="post">
<span class="delete"><a href='#deletionDialog';"><i class="fa fa-trash"></i></a></span>
<span class="edit"><a href="{% url 'post_edit' pk=post.pk %}"><i class="fa fa-pencil"></i></a></span>
<h1>{{ post.title }}</h1> <h1>{{ post.title }}</h1>
<h3> Теги:
{% if tags %}
{% for tag in tags %}
<a href="{% url 'search_by_tag' tag=tag%}">{{ tag }}</a>
{% endfor %}
{% endif %}
</h3>
<p>{{ post.text|safe|linebreaksbr }}</p>
<span>
{% if post.published_date %}
{{ post.published_date }}
{% endif %}
</span>
<span class="edit"><a href="{% url 'post_edit' pk=post.pk %}">edit post</a></span> {% if post.published_date %}
<span class="edit delete"><a href='#deletionDialog';">delete</a></span> <div class="date"><i class="fa fa-calendar"></i> {{ post.published_date}}</div>
{% endif %}
{% if post.tags.names %}
<div class="tag">
<i class="fa fa-tags"></i>
{% for tag in post.tags.names %}
<a href="{% url 'post_list' tag=tag%}">{{ tag }}</a>
{% endfor %}
</div>
{% endif %}
<p>{{ post.text|safe|linebreaksbr }}</p>
<div id="deletionDialog" class="dialog"> <div id="deletionDialog" class="dialog">
<div> <div>
<p align="right"><a href=""><img src="http://py.difrex.ru/media/images/x.PNG"></a></p> <a class="close" href="">x</a>
<form method="POST">{% csrf_token %} <span style="margin: auto">Удалить пост "{{ post.title }}"? </span>
<input type=hidden name="id" value="{{ post.pk }}"> confirm deletion of {{ post.pk }} {{ post.title }} <form method="POST" >{% csrf_token %}
<input type="submit" name="action" value="delete"> <input type=hidden name="id" value="{{ post.pk }}">
<input type="submit" name="action" value="delete" onclick="location.href = '#'">
<input type="button" value="cancel" onclick="location.href='#'">
</form> </form>
</div> </div>
</div> </div>

View File

@ -2,7 +2,7 @@
{% load pagination_tags %} {% load pagination_tags %}
{% block content %} {% block content %}
<div id="posts"> {% if posts %}
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">
<div class="post-header"> <div class="post-header">
@ -12,7 +12,7 @@
<div class="tag"> <div class="tag">
<i class="fa fa-tags"></i> <i class="fa fa-tags"></i>
{% for tag in post.tags.names %} {% for tag in post.tags.names %}
<a href="{% url 'search_by_tag' tag=tag%}">{{ tag }}</a> <a href="{% url 'post_list' tag=tag%}">{{ tag }}</a>
{% endfor %} {% endfor %}
</div> </div>
{% endif %} {% endif %}
@ -20,5 +20,11 @@
{{ post.text|safe|linebreaksbr|truncatewords:80 }} <!--|linebreaksbr|truncatewords:80 }}</p--><br> {{ post.text|safe|linebreaksbr|truncatewords:80 }} <!--|linebreaksbr|truncatewords:80 }}</p--><br>
</div> </div>
{% endfor %} {% endfor %}
{% else %}
<div class="post">
<p>Ничего не найдено</p>
</div> </div>
{% endif %}
{% endblock content %} {% endblock content %}

View File

@ -1,19 +0,0 @@
{% extends 'blog/base.html' %}
{% load pagination_tags %}
{% block content %}
{% for post in tagged_posts %}
<div class="block post">
<h1><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h1>
{{ post.text|safe|linebreaksbr|truncatewords:80 }} <!--|linebreaksbr|truncatewords:80 }}</p--><br>
<p style="float: left; color: #1abc9c; font-style: italic;"> Tags:
{% for tag in post.tags.names %}
<a href="{% url 'search_by_tag' tag=tag%}" style="color: #1abc9c; ">{{ tag }}</a>
{% endfor %}
</p>
<p align="right">
{{ post.published_date}}
</p>
</div>
{% endfor %}
{% endblock content %}

View File

View File

@ -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)

View File

@ -3,10 +3,12 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^$', views.post_list, name='post_list'), url(r'^$', views.post_list, name='post_list'),
url(r'^(?P<page>[0-9]+)/$', views.post_list, name='post_list'), #url(r'^(?P<page>[0-9]+)/$', views.post_list, name='post_list'),
url(r'^tag/(?P<tag>[a-zA-Z]+)/$', views.post_list, name='post_list'),
#url(r'^(?P<tag>[a-zA-Z]+)/(?P<page>[0-9]+)/$', views.post_list, name='post_list'),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'), url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail, name='post_detail'),
url(r'^new/$', views.post_new, name='post_new'), url(r'^new/$', views.post_new, name='post_new'),
url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'), url(r'^post/(?P<pk>[0-9]+)/edit/$', views.post_edit, name='post_edit'),
url(r'^tag/(?P<tag>[a-zA-Z]+)/$', views.search_by_tag, name='search_by_tag'), #url(r'^tag/(?P<tag>[a-zA-Z]+)/$', views.search_by_tag, name='search_by_tag'),
] ]

View File

@ -1,43 +1,48 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.shortcuts import render from django.shortcuts import render
from .models import Post from .models import Post
from django.db.models import Count, Q
from django.utils import timezone from django.utils import timezone
from django.shortcuts import render, get_object_or_404, render_to_response from django.shortcuts import render, get_object_or_404, render_to_response
from django.shortcuts import redirect from django.shortcuts import redirect
from .forms import PostForm from .forms import PostForm
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from taggit.models import Tag
def post_list(request, page='1'): def post_list(request, page='1', tag=None):
post_list = Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')#[int(page)*5-5:int(page)*5] 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) paginator = Paginator(post_list, 5)
page = request.GET.get('page') 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: try:
posts = paginator.page(page) 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) 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) posts = paginator.page(paginator.num_pages)
return render_to_response('blog/post_list.html', {'posts': posts}) print dir(posts.paginator)
return render_to_response('blog/post_list.html', {'posts': posts, 'latest': latest, 'tags': tags})
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): def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk) post = get_object_or_404(Post, pk=pk)
tags = post.tags.names() 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': if request.POST.get('action') == 'delete':
post.delete() if request.user.is_authenticated():
print '------------delete---------------' + pk post.delete()
return redirect('blog.views.post_list', 1) return redirect('/')
return render(request, 'blog/post_detail.html', {'post': post, 'tags': tags}) 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/") @login_required(login_url="/admin/login/?next=/blog/")
def post_new(request): def post_new(request):
@ -54,6 +59,7 @@ def post_new(request):
form = PostForm() form = PostForm()
return render(request, 'blog/post_edit.html', {'form': form}) return render(request, 'blog/post_edit.html', {'form': form})
@login_required(login_url="/admin/login/?next=/blog/") @login_required(login_url="/admin/login/?next=/blog/")
def post_edit(request, pk): def post_edit(request, pk):
post = get_object_or_404(Post, pk=pk) post = get_object_or_404(Post, pk=pk)

71
blog/views.py.save Normal file
View File

@ -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})