Browse Source

by clicking on tags, all the posts with this tag showing up

newsletter
Esther Kleinhenz 6 years ago
parent
commit
768a3e4a2e

+ 6
- 8
application/templates/post_list.html View File

{% 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 %}
<div class="post"> <div class="post">
<div class="date"> <div class="date">
{{ post.published_date }} {{ post.published_date }}
<a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a> <a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a>
</h1> </h1>
<p>{{ post.text|linebreaks }}</p> <p>{{ post.text|linebreaks }}</p>
<p>
{% for tag in post.tags.all %}
<a href="">{{ tag.name }}, </a>
{% endfor %}
</p>
<p>
Tags: {% for tag in post.tags.all %}
<a href="{% url 'post_list_by_tag' tag.slug %}">{{ tag.name }}</a>
{% if not forloop.last %}, {% endif %} {% endfor %} <p>
{{ post.author }} {{ post.author }}
</p> </p>

</div> </div>
{% endfor %} {% endfor %}
</div> </div>

+ 15
- 3
application/templates/student_page.html View File

{% extends "base.html" %} {% block content %}
{% extends "base.html" %} {% block content %} {% load taggit_templatetags2_tags %} {% get_taglist as tags for 'application.post'%}


<div id="">
<ul>
{% for tag in tags %}
<li>{{tag}}
<a class="btn btn-outline-dark" href="{% url 'tag_remove' %}">
<span class="glyphicon glyphicon-remove">Remove</span>
</a>
</li>
{{ result }} {% endfor %}
</ul>
</div>
<div>
</div>
<form id="searchform" action="{% url 'blog_search_list_view' %}" method="get" accept-charset="utf-8"> <form id="searchform" action="{% url 'blog_search_list_view' %}" method="get" accept-charset="utf-8">
<button class="searchbutton" type="submit"> <button class="searchbutton" type="submit">
<i class="fa fa-search"></i> <i class="fa fa-search"></i>
</div> </div>


<form class="post-form" method="post"> <form class="post-form" method="post">
{% csrf_token %}
{{form.as_p}}
{% csrf_token %} {{form.as_p}}
<button type="submit" class="save btn btn-outline-dark">Save</button> <button type="submit" class="save btn btn-outline-dark">Save</button>
</form> </form>
{% endblock %} {% endblock %}

+ 2
- 0
application/urls.py View File



urlpatterns = [ urlpatterns = [
url(r'^$', views.post_list, name='post_list'), url(r'^$', views.post_list, name='post_list'),
url(r'^tag/(?P<slug>[-\w]+)/$', views.post_list, name='post_list_by_tag'),
url(r'^student/', views.student_page, name='student_page'), url(r'^student/', views.student_page, name='student_page'),
url(r'^search/', views.blog_search_list_view, name='blog_search_list_view'), url(r'^search/', views.blog_search_list_view, name='blog_search_list_view'),
url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'), url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
url(r'^drafts/$', views.post_draft_list, name='post_draft_list'), url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'), url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
url(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'), url(r'^post/(?P<pk>\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')), url(r'^tags/', include('taggit_templatetags2.urls')),
] ]



+ 19
- 4
application/views.py View File

from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.utils import timezone from django.utils import timezone
from .models import Post, CustomUser from .models import Post, CustomUser
from taggit.models import Tag
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .forms import PostForm, NewTagForm from .forms import PostForm, NewTagForm
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.db.models import Q from django.db.models import Q
import sys
from taggit_templatetags2.views import TagCanvasListView from taggit_templatetags2.views import TagCanvasListView


import logging import logging




@login_required @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 @login_required
return redirect('post_list') 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 @login_required
def student_page(request): def student_page(request):
user_instance = CustomUser.objects.get(user=request.user) user_instance = CustomUser.objects.get(user=request.user)

Loading…
Cancel
Save