Added post tags

This commit is contained in:
Gergely Polonkai 2013-10-01 21:18:11 +02:00
parent 6ada4d0a5d
commit 4cb97ed97a
5 changed files with 15 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from taggit.managers import TaggableManager
class Post(models.Model): class Post(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
@ -8,6 +9,7 @@ class Post(models.Model):
slug = models.CharField(max_length = 100) slug = models.CharField(max_length = 100)
content = models.TextField() content = models.TextField()
draft = models.BooleanField() draft = models.BooleanField()
tags = TaggableManager()
def __unicode__(self): def __unicode__(self):
return self.title return self.title

View File

@ -2,6 +2,13 @@
<h3>{% if title_links %}<a href="{% url 'blog:read' post.created_at.year post.created_at.month post.created_at.day post.slug %}">{% endif %}{{ post.title }}{% if title_links %}</a>{% else %}<div class="plusone-container"><div class="g-plusone" data-annotation="inline" data-size="small" data-width="300"></div></div>{% endif %}</h3> <h3>{% if title_links %}<a href="{% url 'blog:read' post.created_at.year post.created_at.month post.created_at.day post.slug %}">{% endif %}{{ post.title }}{% if title_links %}</a>{% else %}<div class="plusone-container"><div class="g-plusone" data-annotation="inline" data-size="small" data-width="300"></div></div>{% endif %}</h3>
<p class="article-date">{{ post.created_at|date:'m-d-Y :: H:i' }} by {{ post.user.first_name }} {{ post.user.last_name }}</p> <p class="article-date">{{ post.created_at|date:'m-d-Y :: H:i' }} by {{ post.user.first_name }} {{ post.user.last_name }}</p>
{{ post.content|safe }} {{ post.content|safe }}
{% if post.tags.all %}
<p class="article-tags">Tags:
{% for tag in post.tags.all %}
<a href="{% url 'blog:taglist' tag.slug %}">{{ tag.name }}</a>
{% endfor %}
</p>
{% endif %}
{% if not title_links %} {% if not title_links %}
<div class="g-plus" data-action="share" data-height="15"></div> <div class="g-plus" data-action="share" data-height="15"></div>
{% endif %} {% endif %}

View File

@ -4,6 +4,7 @@ from blog import views
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', views.index, name='index'), url(r'^$', views.index, name='index'),
url(r'^feed$', views.feed, name='feed'), url(r'^feed$', views.feed, name='feed'),
url(r'^tag/(?P<tag>.*)$', views.taglist, name='taglist'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'), url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'),
url(r'^resume', views.resume, name='resume'), url(r'^resume', views.resume, name='resume'),
url(r'^about', views.resume, name='about'), url(r'^about', views.resume, name='about'),

View File

@ -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); 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}) 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): def feed(request):
return render(request, 'blog/feed.xml', {}) return render(request, 'blog/feed.xml', {})

View File

@ -127,6 +127,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.admin', 'django.contrib.admin',
'compressor', 'compressor',
'taggit',
# Uncomment the next line to enable admin documentation: # Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs', # 'django.contrib.admindocs',
'blog', 'blog',