{{ category.name }}
- {% get_recent_published_content as posts %} + {% endif %} {% for post in posts %} @@ -68,11 +71,11 @@{{ post.title }}< - {% empty %} + {% endfor %} -
No posts available.
+ {% else %} - {% endfor %} +No posts available.
{% endif %} diff --git a/djpress/test_category_cache.py b/djpress/test_category_cache.py index 8256c9b..1da7fbe 100644 --- a/djpress/test_category_cache.py +++ b/djpress/test_category_cache.py @@ -17,7 +17,7 @@ def test_get_cached_categories(): Category.objects.create(name="Category 2") # Call the get_cached_categories method - queryset = Category.objects.get_cached_categories() + queryset = Category.objects._get_cached_categories() # Assert that the queryset is cached cached_queryset = cache.get(CATEGORY_CACHE_KEY) @@ -26,7 +26,7 @@ def test_get_cached_categories(): assert len(cached_queryset) == 2 # Assert that subsequent calls retrieve the queryset from cache - queryset2 = Category.objects.get_cached_categories() + queryset2 = Category.objects._get_cached_categories() assert list(queryset2) == list(cached_queryset) @@ -36,7 +36,7 @@ def test_cache_invalidation_on_save(): category = Category.objects.create(name="Category 1") # Call the get_cached_categories method - queryset = Category.objects.get_cached_categories() + queryset = Category.objects._get_cached_categories() # Assert that the queryset is cached cached_queryset = cache.get(CATEGORY_CACHE_KEY) @@ -52,7 +52,7 @@ def test_cache_invalidation_on_save(): assert cached_queryset is None # Call the get_cached_categories method again - queryset2 = Category.objects.get_cached_categories() + queryset2 = Category.objects._get_cached_categories() # Assert that the queryset is cached again with the updated data cached_queryset2 = cache.get(CATEGORY_CACHE_KEY) @@ -67,7 +67,7 @@ def test_cache_invalidation_on_delete(): category = Category.objects.create(name="Category 1") # Call the get_cached_categories method - queryset = Category.objects.get_cached_categories() + queryset = Category.objects._get_cached_categories() # Assert that the queryset is cached cached_queryset = cache.get(CATEGORY_CACHE_KEY) @@ -82,7 +82,7 @@ def test_cache_invalidation_on_delete(): assert cached_queryset is None # Call the get_cached_categories method again - queryset2 = Category.objects.get_cached_categories() + queryset2 = Category.objects._get_cached_categories() # Assert that the queryset is cached again with the updated data cached_queryset2 = cache.get(CATEGORY_CACHE_KEY) diff --git a/djpress/views.py b/djpress/views.py index fcd0b43..8c6d282 100644 --- a/djpress/views.py +++ b/djpress/views.py @@ -8,25 +8,38 @@ def index(request: HttpRequest) -> HttpResponse: """View for the index page.""" - return render(request, "djpress/index.html") + posts = Content.post_objects.get_recent_published_content() + + return render( + request, + "djpress/index.html", + {"posts": posts}, + ) def content_detail(request: HttpRequest, slug: str) -> HttpResponse: """View for a single content page.""" - return render(request, "djpress/index.html", {"slug": slug}) + post = Content.post_objects.get_published_post_by_slug(slug) + + return render( + request, + "djpress/index.html", + {"post": post}, + ) def category_posts(request: HttpRequest, slug: str) -> HttpResponse: """View for posts by category.""" try: - category = Category.objects.get(slug=slug) - posts = Content.post_objects.get_published_content_by_category(category) + category: Category = Category.objects.get_category_by_slug(slug=slug) except Category.DoesNotExist as exc: msg = "Category not found" raise Http404(msg) from exc + posts = Content.post_objects.get_published_content_by_category(category) + return render( request, - "djpress/category_posts.html", - {"category": category, "posts": posts}, + "djpress/index.html", + {"posts": posts, "category": category}, ) diff --git a/templates/base.html b/templates/base.html index eee46c7..4e8afec 100644 --- a/templates/base.html +++ b/templates/base.html @@ -1,11 +1,12 @@ {% load static %} +{% load djpress_tags %} -Category: {{ category.name }}
-{{ category.description }}
-- {% for post in posts %} - {% include "djpress/snippets/content_summary.html" %} - {% empty %} -
No posts available in this category.
- {% endfor %} -{% endblock %} diff --git a/templates/djpress/index.html b/templates/djpress/index.html index 71e7a57..a45b352 100644 --- a/templates/djpress/index.html +++ b/templates/djpress/index.html @@ -1,32 +1,46 @@ {% extends 'base.html' %} {% load djpress_tags %} -{% block title %}Home{% endblock %} -{% block content %} +{% block title %} - {% if slug %} - {% comment %} If there's a slug, we only display a single post. {% endcomment %} + {% if post %} - {% get_single_published_content slug as post %} + {{ post.title }} - {% get_blog_title %} - {% include "djpress/snippets/content_detail.html" %} + {% elif category %} + + {{ category.name }} - {% get_blog_title %} {% else %} - {% comment %} If there's no slug, we loop through recent published content. {% endcomment %} - {% get_recent_published_content as posts %} + {% get_blog_title %} + + {% endif %} + +{% endblock title %} + + +{% block content %} + + {% if post %} + + {% include "djpress/snippets/content_detail.html" %} + + {% elif posts %} + +The posts go here.
{% for post in posts %} {% include "djpress/snippets/content_summary.html" %} - {% empty %} + {% endfor %} -No posts available.
+ {% else %} - {% endfor %} +No posts available.
{% endif %} -{% endblock %} +{% endblock content %} diff --git a/templates/djpress/post.html b/templates/djpress/post.html deleted file mode 100644 index 8e11726..0000000 --- a/templates/djpress/post.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends 'base.html' %} - -{% block title %}{{ post.title }}{% endblock %} - -{% block content %} - - {% include "djpress/snippets/content_detail.html" %} - -- Back to Home -{% endblock %}