Login

Read more link

Author:
nny777
Posted:
July 1, 2008
Language:
Python
Version:
.96
Tags:
template filter blog find jump read more
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. Unobtrusive comment moderation by ubernostrum 8 years, 3 months ago
  2. Find all links in a value and display them separatley by jcroft 8 years, 4 months ago
  3. Hyperlink list filter by lifefloatsby 7 years, 6 months ago
  4. Template filter for internal links in TextFields by bobtiki 7 months, 3 weeks ago
  5. Template filter to convert timecodes into links by justin_h 5 years, 10 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.