Skip to content

Commit

Permalink
Using django-taggit to implement a tagging system
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 22, 2023
1 parent 55ba561 commit 2dc68ee
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 23 deletions.
Binary file modified Module03/mysite/blog/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified Module03/mysite/blog/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module03/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
20 changes: 20 additions & 0 deletions Module03/mysite/blog/migrations/0004_post_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.4 on 2023-08-22 14:35

from django.db import migrations
import taggit.managers


class Migration(migrations.Migration):

dependencies = [
('taggit', '0005_auto_20220424_2025'),
('blog', '0003_comment'),
]

operations = [
migrations.AddField(
model_name='post',
name='tags',
field=taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags'),
),
]
Binary file not shown.
4 changes: 4 additions & 0 deletions Module03/mysite/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
from taggit.managers import TaggableManager


class Post(models.Model):

Expand All @@ -18,6 +20,8 @@ class Status(models.TextChoices):
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT)

tags = TaggableManager()

class Meta:
ordering = ['-publish']
indexes = [
Expand Down
20 changes: 18 additions & 2 deletions Module03/mysite/blog/templates/blog/post/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

{% block content %}
<h1>My Blog</h1>
{% if tag %}
<h2>Posts tagged with "{{ tag.name }}"</h2>
{% endif %}
{% for post in posts %}
<h2>
<a href="{{post.get_absolute_url}}">
{{ post.title }}
{{ post.title }}
</a>
</h2>


<p class="tags">
Tags:
{% for tag in post.tags.all %}
<a href="{% url "blog:post_list_by_tag" tag.slug %}">
{{ tag.name }}
</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
</p>


<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|truncatewords:30|linebreaks }}
<hr />
{% endfor %}
{% include "pagination.html" with page=page_obj %}
{% include "pagination.html" with page=posts %}
{% endblock %}
5 changes: 3 additions & 2 deletions Module03/mysite/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
app_name = 'blog'

urlpatterns = [
# path('', views.post_list, name='post_list'),
path('', views.PostListView.as_view(), name='post_list'),
path('', views.post_list, name='post_list'),
path('tag/<slug:tag_slug>/', views.post_list, name='post_list_by_tag'),
# path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
path('<int:post_id>/share/', views.post_share, name='post_share'),
path('<int:post_id>/comment/', views.post_comment, name='post_comment'),
Expand Down
43 changes: 24 additions & 19 deletions Module03/mysite/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@
from .forms import EmailPostForm, CommentForm
from django.core.mail import send_mail
from django.views.decorators.http import require_POST
from taggit.models import Tag

# def post_list(request):
# post_list = Post.objects.all()
# paginator = Paginator(post_list, 2)
# page_number = request.GET.get('page', 1)
def post_list(request, tag_slug=None):
post_list = Post.objects.all()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
post_list = post_list.filter(tags__in=[tag])

# try:
# posts = paginator.page(page_number)
# except PageNotAnInteger:
# posts = paginator.page(1)
# except EmptyPage:
# posts = paginator.page(paginator.num_pages)
# return render(request, 'blog/post/list.html', {'posts': posts})
paginator = Paginator(post_list, 2)
page_number = request.GET.get('page', 1)
try:
posts = paginator.page(page_number)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/post/list.html', {'posts': posts, 'tag': tag})

class PostListView(ListView):
"""
Alternative post list view
"""
model = Post
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'
# class PostListView(ListView):
# """
# Alternative post list view
# """
# model = Post
# context_object_name = 'posts'
# paginate_by = 3
# template_name = 'blog/post/list.html'

def post_detail(request, year, month, day, post):
post = get_object_or_404(
Expand Down
Binary file modified Module03/mysite/db.sqlite3
Binary file not shown.
Binary file modified Module03/mysite/mysite/__pycache__/settings.cpython-311.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions Module03/mysite/mysite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig',
'taggit',
]

MIDDLEWARE = [
Expand Down

0 comments on commit 2dc68ee

Please sign in to comment.