0

I have been trying to refactor my blog post views form a single custom template/view/url setup to a hopefully better set of Generic Date Views. However this is not working as planned, as my new views aren't picking up the objects. I've tried to read the Generic date views page as thoroughly as I can but I can't it working.

Here's my Urls.py:

"""Creativeflow URL Configuration the blog app."""
from django.conf.urls import url
from django.views.generic.dates import ArchiveIndexView, YearArchiveView
from .models import Post
from .views import BlogDetailView
urlpatterns = [
    url(r'^posts\/(?P<year>\d{4})\/(?P<month>\d{2})\/(?P<day>\d{2})\/(?P<slug>\w+)',
        BlogDetailView.as_view(), name="blog-detail"),
    url(r'^posts/?$',
        ArchiveIndexView.as_view(model=Post, date_field="published"), name="blog-archive-list"),
    url(r'posts/(?P<year>\d{4})',
        YearArchiveView.as_view(model=Post, date_field="published"), name="post_year_archive")
]

I've only implemented two of the generic views, but I'm intending on implementing all of them.

I don't have any customised views, as I'm relying on the generic classes.

My templates:

post_list.html

{% extends "base.html" %}
{% block content %}
    <main class="flexy">
        <div class="sidebar-wrapper col-md-2 col-xs-12">
            <h3>Posts by Date</h3>
            <ul class="sidebar-nav dates">
                {%for dates in view.post_dates%}
                <li class = "sidebar-item dropdown">
                    <a href="/posts/{{dates.year}}" class="dropdown-toggle" id="dropdownMenu{{dates.year}}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">{{dates.year}}</a>
                    <ul class="dropdown-menu" aria-labelledby="dropdownMenu{{dates.year}}">
                        {% for month in dates.months %}
                        <li>
                            <a href="/posts/{{dates.year}}/{{month.num}}">{{month.name}}</a>
                        </li>
                        {% endfor %}
                    </ul>
                </li>
                {%endfor%}
            </ul>
            <h3>Social Media Links</h3>
            <ul class="sidebar-nav social">
                {% for site in view.get_social_media_sites %}
                <li class = "sidebar-item">
                    <a href="{{site.url}}">{{site.name}}</a>
                </li>
                {% endfor %}
            </ul>
        </div>
        <div class="blog-wrapper">
            <div class="page-header">
                <p> Blog List - posts earlier than {{view.query_date}}</p>
            </div>
            {% block list %}
            {% endblock %}
        </div>
    </main>
{% endblock content %}

And the archive page:

post_archive.html

{% extends "blog/post_lists.html" %}
{% block post_list %}
{% for blog in latest %}
    <div class="blog-card panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">{{blog.title}} - {{blog.published}}</h3>
        </div>
        <div class="panel-body">
            <p>{{blog.body|linebreaks}}<p>
        </div>
        <div class="panel-footer">
            <footer>Written by {{blog.author}}</footer>
        </div>
    </div>
{% empty %}
    <p>Nothing to see</p>
{% endfor %}
{% endblock post_list %}

I'm confused as I'd image in that if there were no objects in latest, then the empty block would be triggered, but I've got nothing appearing and no errors in the console.

What is it that I've missed? Is there an easier way of having all the generic dates views (year, month, etc) available?

Community
  • 1
  • 1
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

0 Answers0