From 768a3e4a2e3311a18a8107d6d613676bbb5fd5d0 Mon Sep 17 00:00:00 2001 From: Esther Kleinhenz Date: Sun, 2 Sep 2018 11:58:21 +0200 Subject: [PATCH] by clicking on tags, all the posts with this tag showing up --- application/templates/post_list.html | 14 ++++++-------- application/templates/student_page.html | 18 +++++++++++++++--- application/urls.py | 2 ++ application/views.py | 23 +++++++++++++++++++---- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/application/templates/post_list.html b/application/templates/post_list.html index cd0c6b7..b363d69 100644 --- a/application/templates/post_list.html +++ b/application/templates/post_list.html @@ -1,4 +1,6 @@ -{% extends 'base.html' %} {% block content %} {% for post in posts %} {% load taggit_templatetags2_tags %} +{% extends 'base.html' %} {% block content %} +{% if tag %} Posts tagged with "{{ tag.name }}" {% endif %} +{% for post in posts %} {% load taggit_templatetags2_tags %}
{{ post.published_date }} @@ -7,15 +9,11 @@ {{ post.title }}

{{ post.text|linebreaks }}

-

- {% for tag in post.tags.all %} - {{ tag.name }}, - {% endfor %} -

-

+ Tags: {% for tag in post.tags.all %} + {{ tag.name }} + {% if not forloop.last %}, {% endif %} {% endfor %}

{{ post.author }}

-
{% endfor %}
diff --git a/application/templates/student_page.html b/application/templates/student_page.html index 2f5fab9..8a167db 100644 --- a/application/templates/student_page.html +++ b/application/templates/student_page.html @@ -1,5 +1,18 @@ -{% extends "base.html" %} {% block content %} +{% extends "base.html" %} {% block content %} {% load taggit_templatetags2_tags %} {% get_taglist as tags for 'application.post'%} +
+ +
+
+
{% endblock %} \ No newline at end of file diff --git a/application/urls.py b/application/urls.py index f983477..d059b6a 100644 --- a/application/urls.py +++ b/application/urls.py @@ -5,6 +5,7 @@ from . import views urlpatterns = [ url(r'^$', views.post_list, name='post_list'), + url(r'^tag/(?P[-\w]+)/$', views.post_list, name='post_list_by_tag'), url(r'^student/', views.student_page, name='student_page'), url(r'^search/', views.blog_search_list_view, name='blog_search_list_view'), url(r'^post/(?P\d+)/$', views.post_detail, name='post_detail'), @@ -13,6 +14,7 @@ urlpatterns = [ url(r'^drafts/$', views.post_draft_list, name='post_draft_list'), url(r'^post/(?P\d+)/publish/$', views.post_publish, name='post_publish'), url(r'^post/(?P\d+)/remove/$', views.post_remove, name='post_remove'), + url(r'^student/remove/$', views.tag_remove, name='tag_remove'), url(r'^tags/', include('taggit_templatetags2.urls')), ] diff --git a/application/views.py b/application/views.py index 33f2c93..46e7780 100644 --- a/application/views.py +++ b/application/views.py @@ -1,12 +1,15 @@ from django.shortcuts import render, get_object_or_404 from django.utils import timezone from .models import Post, CustomUser +from taggit.models import Tag +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from .forms import PostForm, NewTagForm from django.shortcuts import redirect from django.contrib.auth.decorators import login_required from django.contrib.admin.views.decorators import staff_member_required from django.contrib.auth import authenticate, login, logout from django.db.models import Q +import sys from taggit_templatetags2.views import TagCanvasListView import logging @@ -42,10 +45,13 @@ def navlogin(request): @login_required -def post_list(request): - posts = Post.objects.filter( - published_date__lte=timezone.now()).order_by('published_date') - return render(request, 'post_list.html', {'posts': posts}) +def post_list(request, slug=None): + posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date') + if slug: + tag = get_object_or_404(Tag, slug=slug) + posts= posts.filter(tags__in=[tag]) + page = request.GET.get('page') + return render(request, 'post_list.html', locals()) @login_required @@ -110,6 +116,15 @@ def post_remove(request, pk): return redirect('post_list') +@login_required +def tag_remove(request): + tag_to_delete = CustomUser.objects.get(tags) + obj.user = request.user + obj.save() + obj.tags.tag_remove(tag_to_delete) + obj.save_m2m() + return redirect('student_page') + @login_required def student_page(request): user_instance = CustomUser.objects.get(user=request.user)