Skip to content

Commit

Permalink
Add Tags and navigation through tags
Browse files Browse the repository at this point in the history
Allows user to pick or create Tags in the admin, The user can then apply these Tags to blog posts. Clicking on a Tag will link to a page with allposts of said tag. Closes issue Djenesis#56.
  • Loading branch information
Heasummn committed Sep 24, 2015
1 parent 103d1f7 commit 224d5ec
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
3 changes: 2 additions & 1 deletion contributr/contriblog/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from .models import Post
from .models import *


class PostAdmin(admin.ModelAdmin):
Expand All @@ -9,3 +9,4 @@ class PostAdmin(admin.ModelAdmin):


admin.site.register(Post, PostAdmin)
admin.site.register(Tag)
26 changes: 26 additions & 0 deletions contributr/contriblog/migrations/0002_auto_20150924_1705.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('contriblog', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Tag',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('slug', models.SlugField(unique=True, max_length=200)),
],
),
migrations.AddField(
model_name='post',
name='tags',
field=models.ManyToManyField(to='contriblog.Tag'),
),
]
7 changes: 7 additions & 0 deletions contributr/contriblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from django.core.urlresolvers import reverse

# Tag class
class Tag(models.Model):
slug = models.SlugField(max_length=200, unique=True)

def __str__(self):
return self.slug

# Custom queryset manager that only displays posts that have publish set to true.
class PostQuerySet(models.QuerySet):
Expand All @@ -18,6 +24,7 @@ class Post(models.Model):
edited_date = models.DateTimeField(auto_now=True)
publish = models.BooleanField(default=False)
slug = models.SlugField(max_length=200, unique=True)
tags = models.ManyToManyField(Tag)

objects = PostQuerySet.as_manager()

Expand Down
10 changes: 9 additions & 1 deletion contributr/contriblog/templates/contriblog/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@
<div class="page-header">
<h1>{{ object.title }}</h1>
</div>
<p class="text-muted">created on {{ object.created_date|date }}, by {{ object.author }}</p>

<p class="text-muted">
created on {{ object.created_date|date }}, by {{ object.author }} | Tagged under
{% for tag in object.tags.all %}
<a href="{% url "blog:tag_detail" tag=tag.slug %}">{{ tag.slug }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
</p>

<p>{{ object.body|markdown }}</p>

</div>
</div>
{% endblock %}
8 changes: 7 additions & 1 deletion contributr/contriblog/templates/contriblog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ <h1>Contributr blog</h1>
</div>
{% for object in object_list %}
<h3><a href="{% url "blog:detail" slug=object.slug %}">{{ object.title }}</a></h3>
<p class="text-muted">created on {{ object.created_date|date }}, by {{ object.author }}</p>

<p class="text-muted">
created on {{ object.created_date|date }}, by {{ object.author }} | Tagged under
{% for tag in object.tags.all %}
<a href="{% url "blog:tag_detail" tag=tag.slug %}">{{ tag.slug }}</a>{% if not forloop.last %},{% endif %}
{% endfor %}
</p>

<p>{{ object.body|markdown }}</p>
</div>
Expand Down
11 changes: 11 additions & 0 deletions contributr/contriblog/templates/contriblog/tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "base.html" %}
{% block content %}
<div class="post">
<h2>Tagged under {{ object.slug }}</h2>
<ul>
{% for entry in object.post_set.all %}
<li><a href="{{ entry.get_absolute_url }}">{{ entry.title }}</a></li>
{% endfor %}
</ul>
</div>
{% endblock %}
1 change: 1 addition & 0 deletions contributr/contriblog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

urlpatterns = [
url(r'^$', views.BlogIndex.as_view(), name="index"),
url(r'^tags/(?P<tag>\S+)$', views.TagDetail.as_view(), name="tag_detail"),
url(r'^post/(?P<slug>\S+)$', views.BlogDetail.as_view(), name="detail"),
]
6 changes: 6 additions & 0 deletions contributr/contriblog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ class BlogIndex(generic.ListView):
class BlogDetail(generic.DetailView):
model = models.Post
template_name = "contriblog/detail.html"

# Generic view that lists every post with selected tag.
class TagDetail(generic.DetailView):
model = models.Tag
template_name = "contriblog/tag.html"
slug_url_kwarg = "tag"

0 comments on commit 224d5ec

Please sign in to comment.