================================ How to create a Blog application ================================ A Blog is made of dated Blog Posts. It so happens this CMS pages model contain a creation date and is flexible enough to contain all sort contents that could be useful in a Blog post. So how can we use this CMS to create a Blog application? This guide gives a step by step recipe that demonstrate how easily you can build new features on top of this CMS. Step 1: Create a new Django app within your project ------------------------------------------------------ .. code-block:: bash python manage.py startapp blog Step 2: Create the views --------------------------- Open you newly created `blog/views.py` file and create 2 views. One for the category view and another for the Blog's index. We are using the `django-taggit` application to handle the categories, this way a Blog Post can be included in several categories. .. code-block:: python from django.shortcuts import render from pages.models import Page from taggit.models import Tag from django.core.paginator import Paginator def category_view(request, *args, **kwargs): context = dict(kwargs) category = Tag.objects.get(id=kwargs['tag_id']) page = context['current_page'] blogs = page.get_children_for_frontend().filter(tags__name__in=[category.name]) paginator = Paginator(blogs, 8) page_index = request.GET.get('page') blogs = paginator.get_page(page_index) context['blogs'] = blogs context['category'] = category.name return render(request, 'blog-home.html', context) def blog_index(request, *args, **kwargs): context = dict(kwargs) page = context['current_page'] blogs = page.get_children_for_frontend() paginator = Paginator(blogs, 7) page = request.GET.get('page') blogs = paginator.get_page(page) context['blogs'] = blogs context['template_name'] = 'blog-home.html' return render(request, 'blog-home.html', context) Step 2: Link the views with the CMS ------------------------------------ Populate `blog/urls.py` with those urls .. code-block:: python from django.conf.urls import url from blog import views from django.urls import include, path, re_path urlpatterns = [ url(r'^category/(?P[0-9]+)$', views.category_view, name='blog_category_view'), url(r'^$', views.blog_index, name='blog_index') ] Then the last step is to register this URL module with the CMS. Place this code at the top of you project `urls.py` file. .. code-block:: python from pages.urlconf_registry import register_urlconf register_urlconf('blog', 'blog.urls', label='Blog index') Step 3: Create the blog templates ------------------------------------ You will need create 3 templates for your blog application. The first one is a helper template called `blog-card.html`. It contains a basic representation of a blog as a card: .. code-block:: html+django {% load pages_tags static i18n humanize thumbnail %}
{% get_content page "lead-image" as image %} {% if image %} {% thumbnail image "320x240" crop="center" as img %} {% endthumbnail %} {% else %} {% endif %}

{% show_content page "title" %}

{% show_content page "lead" %}

{% if forloop.first %} {% get_content page "content" as content %}

{{ content | striptags | safe | truncatechars:220 }}

{% endif %}

Published {{ page.creation_date | naturalday }} {% if page.tags.count %} in the categories: {% for tag in page.tags.all %} {{ tag.name }}{% if not forloop.last %},{% endif %} {% endfor %} {% endif %} by {{ page.author.first_name }} {{ page.author.last_name }}

The second is the `blog-home.html` referenced by the views you previoulsy wrote, it will be used by the index and the categories: .. code-block:: html+django {% extends 'index.html' %} {% load pages_tags static i18n humanize thumbnail %} {% block header %}

{% placeholder "title" %} {{ category }}

{% placeholder "lead" with Textarea %}

{% endblock %} {% block content %}
{% for page in blogs %} {% include "blog-card.html" %} {% endfor %}
{% endblock %} Finaly the last one is for the Blog Post itself. You could have different Blog Post templates but for now we only need one, let's call it `blog-post.html`: .. code-block:: html+django {% extends 'index.html' %} {% load pages_tags static i18n humanize %} {% block header %}

{% placeholder "title" %}

{% placeholder "lead" with Textarea %}

Published {{ current_page.creation_date | naturalday }} {% if current_page.tags.count %} in the categories: {% for tag in current_page.tags.all %} {{ tag.name }}{% if not forloop.last %},{% endif %} {% endfor %} {% endif %} by {{ current_page.author.first_name }} {{ current_page.author.last_name }}

{% endblock %} {% block content %}
{% imageplaceholder 'lead-image' block %} {% if content %} {% endif %} {% endplaceholder %}
{% placeholder "content" with RichTextarea %}
{% endblock %} To finish things up you need to allow those 2 templates to be selected by the CMS. Add them to your `PAGE_TEMPLATES` setting: .. code-block:: python PAGE_TEMPLATES = ( ('index.html', 'Default template'), ('blog-post.html', 'Blog post'), ('blog-home.html', 'Blog home'), ) Step 4: Activate the Blog in the admin ------------------------------------------ You can now activate the Blog in the CMS admin. To do so follow those few steps: 1. Create a new page named "Blog", chose the template "Blog Home", and the option "Delegate to application: Blog Index". 2. Add a couple of child pages to this Blog Index page and chose the "Blog Post" template as their template. The result should be a functional blog with an index page, category pages, tagging and pagination. You are also free to create serveral Blog instances withing the CMS by repeating a version of this step. There is no restrictions. `A fully functionnal version of this Blog application is available `_