Add navigation bar and fix the bug of "too many values to unbound"
This commit is contained in:
parent
4d89ccefb5
commit
16c2c73389
|
@ -1,6 +1,7 @@
|
||||||
{% extends "templates/base.html" %}
|
{% extends "templates/base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
<a href="/blog/{{ blog.id }}?page=0">Home</a> <a href="/category/{{ blog.id }}">Categories</a> <a href="/tag/{{ blog.id }}">Tags</a><br/><br/>
|
||||||
{% if categories|length == 0 %}
|
{% if categories|length == 0 %}
|
||||||
<b>Any category available</b><br/><br/>
|
<b>Any category available</b><br/><br/>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
|
@ -31,4 +31,8 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</table>
|
</table>
|
||||||
|
<br/><br/>
|
||||||
|
{% autoescape off %}
|
||||||
|
<center>{{ navigation_bar }}</center>
|
||||||
|
{% endautoescape %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{% extends "templates/base.html" %}
|
{% extends "templates/base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
<a href="/blog/{{ blog.id }}?page=0">Home</a> <a href="/category/{{ blog.id }}">Categories</a> <a href="/tag/{{ blog.id }}">Tags</a><br/><br/>
|
||||||
{% if tags|length == 0 %}
|
{% if tags|length == 0 %}
|
||||||
<b>Any tag available</b><br/><br/>
|
<b>Any tag available</b><br/><br/>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{% extends "templates/base.html" %}
|
{% extends "templates/base.html" %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<a href="/blog/{{ blog.id }}">Home</a> <a href="/category/{{ blog.id }}">Categories</a> <a href="/tag/{{ blog.id }}">Tags</a>
|
<a href="/blog/{{ blog.id }}?page=0">Home</a> <a href="/category/{{ blog.id }}">Categories</a> <a href="/tag/{{ blog.id }}">Tags</a>
|
||||||
{% if user.is_superuser %}
|
{% if user.is_superuser %}
|
||||||
<form action="/blog/edit/{{ blog.id }}" method="post">
|
<form action="/blog/edit/{{ blog.id }}" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
@ -24,4 +24,8 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</table>
|
</table>
|
||||||
|
<br/><br/>
|
||||||
|
{% autoescape off %}
|
||||||
|
<center>{{ navigation_bar }}</center>
|
||||||
|
{% endautoescape %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
85
views.py
85
views.py
|
@ -55,6 +55,29 @@ def have_I_right(request, blog_id=None, post_id=None, must_be_superuser=False):
|
||||||
|
|
||||||
return (b, p)
|
return (b, p)
|
||||||
|
|
||||||
|
def createNavigationBar(blog_id, cur_page, nb_pages):
|
||||||
|
navigation_bar = ''
|
||||||
|
if cur_page == 0:
|
||||||
|
navigation_bar += '<< <'
|
||||||
|
else:
|
||||||
|
navigation_bar += '<a href="/blog/%d?page=0"><<</a> ' % blog_id
|
||||||
|
navigation_bar += '<a href="/blog/%d?page=%d"><</a> ' % (blog_id, cur_page-1)
|
||||||
|
|
||||||
|
for i in range(nb_pages+1):
|
||||||
|
if i == cur_page:
|
||||||
|
navigation_bar += ' %d' % (i+1)
|
||||||
|
else:
|
||||||
|
navigation_bar += ' <a href="/blog/%d?page=%d">%d</a>' % (blog_id, i, i+1)
|
||||||
|
|
||||||
|
if cur_page == nb_pages:
|
||||||
|
navigation_bar += ' > >>'
|
||||||
|
else:
|
||||||
|
navigation_bar += ' <a href="/blog/%d?page=%d">></a>' % (blog_id, nb_pages)
|
||||||
|
navigation_bar += ' <a href="/blog/%d?page=%d">>></a>' % (blog_id, nb_pages)
|
||||||
|
|
||||||
|
return navigation_bar
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated():
|
||||||
return HttpResponseRedirect('/blog')
|
return HttpResponseRedirect('/blog')
|
||||||
|
@ -150,17 +173,17 @@ def edit_user(request, user_id):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def category(request, blog_id):
|
def category(request, blog_id):
|
||||||
b = have_I_right(request, blog_id)
|
b,_ = have_I_right(request, blog_id)
|
||||||
|
|
||||||
categories = Category.objects.filter(blog_id=blog_id)
|
categories = Category.objects.filter(blog_id=blog_id)
|
||||||
|
|
||||||
c = {'categories' : categories}
|
c = {'categories' : categories, 'blog' : b}
|
||||||
|
|
||||||
return render(request, 'templates/category.html', c)
|
return render(request, 'templates/category.html', c)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def add_category(request, blog_id):
|
def add_category(request, blog_id):
|
||||||
b = have_I_right(request, blog_id)[0]
|
b,_ = have_I_right(request, blog_id)
|
||||||
|
|
||||||
if request.method == 'POST': # If the form has been submitted...
|
if request.method == 'POST': # If the form has been submitted...
|
||||||
if 'add' in request.POST:
|
if 'add' in request.POST:
|
||||||
|
@ -187,7 +210,7 @@ def edit_category(request, category_id):
|
||||||
if category is None:
|
if category is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
b = have_I_right(request, category.blog.id)[0]
|
b,_ = have_I_right(request, category.blog.id)
|
||||||
|
|
||||||
if request.method == 'POST': # If the form has been submitted...
|
if request.method == 'POST': # If the form has been submitted...
|
||||||
if 'cancel' in request.POST:
|
if 'cancel' in request.POST:
|
||||||
|
@ -215,7 +238,7 @@ def delete_category(request, category_id):
|
||||||
if category is None:
|
if category is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
b = have_I_right(request, category.blog.id)[0]
|
b,_ = have_I_right(request, category.blog.id)
|
||||||
|
|
||||||
category.remove()
|
category.remove()
|
||||||
category.delete()
|
category.delete()
|
||||||
|
@ -224,11 +247,11 @@ def delete_category(request, category_id):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def tag(request, blog_id):
|
def tag(request, blog_id):
|
||||||
b = have_I_right(request, blog_id)
|
b,_ = have_I_right(request, blog_id)
|
||||||
|
|
||||||
tags = Tag.objects.filter(blog_id=blog_id)
|
tags = Tag.objects.filter(blog_id=blog_id)
|
||||||
|
|
||||||
c = {'tags' : tags}
|
c = {'tags' : tags, 'blog' : b}
|
||||||
|
|
||||||
return render(request, 'templates/tag.html', c)
|
return render(request, 'templates/tag.html', c)
|
||||||
|
|
||||||
|
@ -239,7 +262,7 @@ def edit_tag(request, tag_id):
|
||||||
if tag is None:
|
if tag is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
b = have_I_right(request, tag.blog.id)[0]
|
b,_ = have_I_right(request, tag.blog.id)
|
||||||
|
|
||||||
if request.method == 'POST': # If the form has been submitted...
|
if request.method == 'POST': # If the form has been submitted...
|
||||||
if 'cancel' in request.POST:
|
if 'cancel' in request.POST:
|
||||||
|
@ -266,7 +289,7 @@ def delete_tag(request, tag_id):
|
||||||
if tag is None:
|
if tag is None:
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
b = have_I_right(request, tag.blog.id)[0]
|
b,_ = have_I_right(request, tag.blog.id)
|
||||||
|
|
||||||
tag.remove(b)
|
tag.remove(b)
|
||||||
tag.delete()
|
tag.delete()
|
||||||
|
@ -307,16 +330,27 @@ def add_blog(request):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def view_blog(request, blog_id):
|
def view_blog(request, blog_id):
|
||||||
if not request.user.is_superuser:
|
b,_ = have_I_right(request, blog_id)
|
||||||
b = Blog.objects.filter(id=blog_id).filter(writers=request.user.id)
|
|
||||||
|
count = Post.objects.filter(blog=b).count()
|
||||||
|
nb_pages = int(count/50)
|
||||||
|
if 'page' in request.GET:
|
||||||
|
cur_page = int(request.GET['page'])
|
||||||
else:
|
else:
|
||||||
b = Blog.objects.get(pk=blog_id)
|
if 'cur_page' in request.session:
|
||||||
|
cur_page = request.session['cur_page']
|
||||||
|
else:
|
||||||
|
cur_page = 0
|
||||||
|
|
||||||
if b is None:
|
if cur_page < 0 : cur_page = 0
|
||||||
raise Http404
|
if cur_page > nb_pages : cur_page = nb_pages-1
|
||||||
|
|
||||||
posts = Post.objects.filter(blog=b).order_by('-creation_date')
|
request.session['cur_page'] = cur_page
|
||||||
b = Blog.objects.get(pk=blog_id)
|
|
||||||
|
start = cur_page * 50
|
||||||
|
end = start + 50
|
||||||
|
|
||||||
|
posts = Post.objects.filter(blog=b).order_by('-creation_date')[start:end]
|
||||||
form = BlogForm(instance=b)
|
form = BlogForm(instance=b)
|
||||||
|
|
||||||
comments = Comment.objects.all()
|
comments = Comment.objects.all()
|
||||||
|
@ -328,7 +362,9 @@ def view_blog(request, blog_id):
|
||||||
else:
|
else:
|
||||||
dict_comments[key] = dict_comments[key] + 1
|
dict_comments[key] = dict_comments[key] + 1
|
||||||
|
|
||||||
c = {'blog' : b, 'posts' : posts, 'form' : form, 'comments' : dict_comments}
|
navigation_bar = createNavigationBar(b.id, cur_page, nb_pages)
|
||||||
|
|
||||||
|
c = {'blog' : b, 'posts' : posts, 'form' : form, 'comments' : dict_comments, 'navigation_bar' : navigation_bar}
|
||||||
|
|
||||||
return render(request, 'templates/view_blog.html', c)
|
return render(request, 'templates/view_blog.html', c)
|
||||||
|
|
||||||
|
@ -389,7 +425,6 @@ def add_post(request, blog_id):
|
||||||
@login_required
|
@login_required
|
||||||
def edit_post(request, post_id):
|
def edit_post(request, post_id):
|
||||||
(b, post) = have_I_right(request, None, post_id)
|
(b, post) = have_I_right(request, None, post_id)
|
||||||
post = Post.objects.get(pk=post_id)
|
|
||||||
|
|
||||||
title = post.title
|
title = post.title
|
||||||
blog_id = b.id
|
blog_id = b.id
|
||||||
|
@ -434,20 +469,20 @@ def edit_post(request, post_id):
|
||||||
def delete_post(request, post_id):
|
def delete_post(request, post_id):
|
||||||
(b, post) = have_I_right(request, None, post_id)
|
(b, post) = have_I_right(request, None, post_id)
|
||||||
|
|
||||||
blog_id = b.id
|
|
||||||
|
|
||||||
post.delete()
|
post.delete()
|
||||||
|
|
||||||
return HttpResponseRedirect('/blog/' + str(blog_id))
|
return HttpResponseRedirect('/blog/' + str(b.id))
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def generate(request, blog_id):
|
def generate(request, blog_id):
|
||||||
(b, post) = have_I_right(request, blog_id)
|
b,_ = have_I_right(request, blog_id)
|
||||||
|
|
||||||
b.create_paths()
|
b.create_paths()
|
||||||
report = b.generate()
|
report = b.generate()
|
||||||
|
|
||||||
posts = Post.objects.filter(blog=b).order_by('-creation_date')
|
count = Post.objects.filter(blog=b).count()
|
||||||
|
nb_pages = int(count/50)
|
||||||
|
posts = Post.objects.filter(blog=b).order_by('-creation_date')[0:50]
|
||||||
b = Blog.objects.get(pk=blog_id)
|
b = Blog.objects.get(pk=blog_id)
|
||||||
form = BlogForm(instance=b)
|
form = BlogForm(instance=b)
|
||||||
|
|
||||||
|
@ -460,7 +495,9 @@ def generate(request, blog_id):
|
||||||
else:
|
else:
|
||||||
dict_comments[key] = dict_comments[key] + 1
|
dict_comments[key] = dict_comments[key] + 1
|
||||||
|
|
||||||
c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report, 'comments' : dict_comments}
|
navigation_bar = createNavigationBar(b.id, 0, nb_pages)
|
||||||
|
|
||||||
|
c = {'blog' : b, 'posts' : posts, 'form' : form, 'report': report, 'comments' : dict_comments, 'navigation_bar' : navigation_bar}
|
||||||
|
|
||||||
return render(request, 'templates/generate.html', c)
|
return render(request, 'templates/generate.html', c)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user