NameNotFound
NameNotFound

Reputation: 29

Django Taggable tags not working, empty page

I apologize in advance if I write something unclear, I am new to both django and stackoverflow. I'm trying to implement a blog site, with posts and all that. An important part of the site is tag search, which I'm trying to implement via Taggit. However, when I click on the tag url link, I always get a blank page. I also have a pagination system that divides the blog into different pages.

urlpatterns = [
    path('', views.PostList.as_view(), name="home"),
    path(r'^$', views.post_list, name='post_list'),
    path(r'^tag/(?P<tag_slug>[-\w]+)/$', views.post_list, name='post_list_by_tag'),
]
class Post(models.Model):
    title = models.CharField(max_length = 250, unique = True)
    slug = models.CharField(max_length = 200, unique = True)

    type = models.CharField(max_length=100, choices=Type_CHOICES)
    section = models.CharField(max_length=100, choices=Section_CHOICES)
    tags = TaggableManager()
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Post
from django.views import generic
from taggit.models import Tag
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def post_list(request, tag_slug=None):
    object_list = Post.objects.all()
    tag = None

    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        object_list = object_list.filter(tags__in=[tag])

    paginator = Paginator(object_list, 3) # 3 posts in each page
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer deliver the first page
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range deliver last page of results
        posts = paginator.page(paginator.num_pages)
    return render(request, 'index.html', {'page': page,
                                                   'posts': posts,
                                                   'tag': tag,
                                                    '/media': Post.image})
<p>Tags:
  {% for tag in post.tags.all %}
       <a class="tag" href="{% url 'post_list_by_tag' tag %}">
           {{ tag.name }}
        </a>
       {% if not forloop.last %}, {% endif %}
  {% endfor %}
</p>

As soon as I tried to rewrite it and change the urls and views, the result was the same - a blank page.

Upvotes: 1

Views: 92

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476493

Well part of the problem is that you define the pattern with a regex, but path(..) has special syntax, not a regex:

We can define the path as:

urlpatterns = [
    path('', views.post_list, name='home'),
    path('tag/<slug:tag_slug>/', views.post_list, name='post_list_by_tag'),
]

with as view:

def post_list(request, tag_slug=None):
    object_list = Post.objects.all()
    tag = None

    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        object_list = object_list.filter(tags=tag)

    paginator = Paginator(object_list, 3)  # 3 posts in each page
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer deliver the first page
        posts = paginator.page(1)
    except EmptyPage:
        # If page is out of range deliver last page of results
        posts = paginator.page(paginator.num_pages)
    return render(
        request, 'index.html', {'page': page, 'posts': posts, 'tag': tag}
    )

Upvotes: 1

Related Questions