From 4cb97ed97a35c4dd02bc5f483e0791bf9de3e3fc Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 1 Oct 2013 21:18:11 +0200 Subject: [PATCH] Added post tags --- blog/models.py | 2 ++ blog/templates/blog/post.html | 7 +++++++ blog/urls.py | 1 + blog/views.py | 4 ++++ gergelypolonkai_django/settings.py | 1 + 5 files changed, 15 insertions(+) diff --git a/blog/models.py b/blog/models.py index 5ccee54..d397d46 100644 --- a/blog/models.py +++ b/blog/models.py @@ -1,5 +1,6 @@ from django.db import models from django.contrib.auth.models import User +from taggit.managers import TaggableManager class Post(models.Model): user = models.ForeignKey(User) @@ -8,6 +9,7 @@ class Post(models.Model): slug = models.CharField(max_length = 100) content = models.TextField() draft = models.BooleanField() + tags = TaggableManager() def __unicode__(self): return self.title diff --git a/blog/templates/blog/post.html b/blog/templates/blog/post.html index e214675..b129286 100644 --- a/blog/templates/blog/post.html +++ b/blog/templates/blog/post.html @@ -2,6 +2,13 @@

{% if title_links %}{% endif %}{{ post.title }}{% if title_links %}{% else %}
{% endif %}

{{ post.created_at|date:'m-d-Y :: H:i' }} by {{ post.user.first_name }} {{ post.user.last_name }}

{{ post.content|safe }} +{% if post.tags.all %} +

Tags: +{% for tag in post.tags.all %} + {{ tag.name }} +{% endfor %} +

+{% endif %} {% if not title_links %}
{% endif %} diff --git a/blog/urls.py b/blog/urls.py index cedd461..a0f8588 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -4,6 +4,7 @@ from blog import views urlpatterns = patterns('', url(r'^$', views.index, name='index'), url(r'^feed$', views.feed, name='feed'), + url(r'^tag/(?P.*)$', views.taglist, name='taglist'), url(r'^(?P\d+)/(?P\d+)/(?P\d+)/(?P[^/]+)$', views.read, name='read'), url(r'^resume', views.resume, name='resume'), url(r'^about', views.resume, name='about'), diff --git a/blog/views.py b/blog/views.py index 6b5c8b2..2fab154 100644 --- a/blog/views.py +++ b/blog/views.py @@ -10,6 +10,10 @@ def read(request, year, month, day, slug): post = get_object_or_404(Post, created_at__year=int(year), created_at__month=int(month), created_at__day=int(day), slug=slug); return render(request, 'blog/view.html', {'post': post}) +def taglist(request, tag): + posts = Post.objects.filter(tags__slug=tag) + return render(request, 'blog/listing.html', {'posts': posts}) + def feed(request): return render(request, 'blog/feed.xml', {}) diff --git a/gergelypolonkai_django/settings.py b/gergelypolonkai_django/settings.py index 1e668a8..3a088a0 100644 --- a/gergelypolonkai_django/settings.py +++ b/gergelypolonkai_django/settings.py @@ -127,6 +127,7 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'django.contrib.admin', 'compressor', + 'taggit', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', 'blog',