Login

Read more link

Author:
nny777
Posted:
July 1, 2008
Language:
Python
Version:
.96
Score:
-1 (after 7 ratings)

I couldn't find any code for a blog-style "Read more after the jump," so I made a custom filter. It will look for <!--more--> for the jump, like in Wordpress.

In settings.py set READ_MORE_TEXT to what you want the text of the link to be.

READ_MORE_TEXT = 'Read more after the jump.'

When you call the filter in your template, pass it the absolute link of that entry. Of course, you have to have your get_absolute_url function defined in your model, but you should have that already, right? :P

In this example entry.body is the content of the blog entry.

{% load blog_filters %}

{{ entry.body|read_more:entry.get_absolute_url }}

If anyone has a better way to do this, it is, of course, welcome.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#myapp/templatetags/blog_filters.py

from django import template
register = template.Library()
import settings

@register.filter('read_more')
def read_more(body, absolute_url):
	if '<!--more-->' in body:
		return body[:body.find('<!--more-->')]+'<a href="'+str(absolute_url)+'">'+str(settings.READ_MORE_TEXT)+'</a>'
	else:
		return body

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

svetlyak (on September 4, 2008):

I'll rewrite this as:

def read_more(body, absolute_url):
    pos = body.find('<!--more-->')
    if pos == -1:
        return body
    else:
        return '<a href="%s">%s</a>' % (body[:pos], settings.READ_MORE_TEXT))

#

Please login first before commenting.