Create blog RSS feed functionality

Fix feed link in main template
This commit is contained in:
Gergely Polonkai 2014-06-15 10:06:55 +02:00
parent 34d1cefe70
commit f4bfd822f7
3 changed files with 57 additions and 2 deletions

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
{% load static from staticfiles %}
{% load get_post_link from blog_link %}
<rss version="2.0">
<channel>
<title>Gergely Polonkai</title>
<description>Gergely Polonkais Blog</description>
<link>{{ site_url }}</link>
{% comment %}
The <pubDate> element defines the last publication date for the content in the RSS feed.
The <lastBuildDate> element defines the last-modified date of the content of the feed.
TODO: Incorporate a modified_at field in the blog. These fields must depend on them.
{% endcomment %}
<lastBuildDate>{{ last_build_date }}</lastBuildDate>
<pubDate>{{ last_build_date }}</pubDate>
<ttl>7200</ttl>
<language>en</language>
<image>
<title>Gergely Polonkai</title>
<url>{{ profile_pic }}</url>
<link>{{ site_url }}</link>
</image>
{% for post in posts %}
<item>
<title><![CDATA[ {{ post.title }} ]]></title>
<link>{% get_post_link post %}</link>
<comments>{% get_post_link post %}#comments</comments>
<pubDate>{{ post.created_at_rss }}</pubDate>
<description><![CDATA[ {{ post.content|safe }} ]]></description>
<guid>{% get_post_link post %}</guid>
</item>
{% endfor %}
</channel>
</rss>

View File

@ -1,6 +1,10 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from blog.models import Post, CodeChunk from blog.models import Post, CodeChunk
from django.core.urlresolvers import reverse
import datetime
import pytz
from django.templatetags.static import static
def mainpage(request): def mainpage(request):
last_posts = Post.objects.filter(draft = False).order_by('-created_at')[:5] last_posts = Post.objects.filter(draft = False).order_by('-created_at')[:5]
@ -96,5 +100,21 @@ def codechunk(request, language, slug):
return render(request, 'blog/code-chunk.html', {'codechunk': chunk}) return render(request, 'blog/code-chunk.html', {'codechunk': chunk})
def feed(request): def feed(request):
return render(request, 'blog/feed.xml', {}) latest_post = Post.objects.filter(draft = False).order_by('-created_at')[0:1]
if not latest_post:
latest_date = datetime.datetime(1983, 3, 7, 11, 54, 45, 0, pytz.timezone('Europe/Budapest'))
else:
latest_date = latest_post[0].created_at
posts = Post.objects.order_by('-created_at')[:10]
return render(request, 'blog/feed.xml', {
'site_url': request.build_absolute_uri(reverse('home')),
'profile_pic': request.build_absolute_uri(static('images/profile.png')),
'last_build_date': latest_date.strftime('%a, %d %b %Y %T %z'),
'posts': posts,
},
content_type = 'application/xml'
)

View File

@ -15,7 +15,7 @@
<link rel="stylesheet" type="text/css" href="{% static "css/code.css" %}" media="screen" /> <link rel="stylesheet" type="text/css" href="{% static "css/code.css" %}" media="screen" />
{% endblock %} {% endblock %}
{% endcompress %} {% endcompress %}
<link rel="alternate" type="application/rss+xml" title="Gergely Polonkai's Blog - RSS Feed" href="{{ app.request.scheme }}://{{ app.request.host }}{% url 'blog:feed' %}" /> <link rel="alternate" type="application/rss+xml" title="Gergely Polonkai's Blog - RSS Feed" href="//{{ request.META.SERVER_NAME }}{% url 'blog:feed' %}" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head> </head>
<body> <body>