Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:29:00+00:00 2026-06-10T18:29:00+00:00

views.py from django import template register = template.Library() @register.filter def truncatesmart(value, limit=80): Truncates a

  • 0

views.py

from django import template
register = template.Library()

@register.filter
def truncatesmart(value, limit=80):
    """
    Truncates a string after a given number of chars keeping whole words.

    Usage:
        {{ string|truncatesmart }}
        {{ string|truncatesmart:50 }}
    """

    try:
        limit = int(limit)
    # invalid literal for int()
    except ValueError:
        # Fail silently.
        return value

    # Make sure it's unicode
    value = unicode(value)

    # Return the string itself if length is smaller or equal to the limit
    if len(value) <= limit:
        return value

    # Cut the string
    value = value[:limit]

    # Break into words and remove the last
    words = value.split(' ')[:-1]

    # Join the words and return
    return ' '.join(words) + '...'

html

{% block content %}

<div class="container-fluid">
    <div class="container" id="content">
        <div class="span3">
            <div class="dashboard">
                <div class="well smooth-edge2 shadow">
                    <div class="mini-info">
                        <div class="username">
                            <h2 class="text-center">{{rest.name|truncatesmart}}</h2>

{% endblock %}

error

TemplateSyntaxError at /rprofile/info
Invalid filter: 'truncatesmart'

doubt

I am not able to understand why this custom filter is not working. While all other predefined filters such as title are working, this custom filter is not working at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:29:02+00:00Added an answer on June 10, 2026 at 6:29 pm

    According to the documentation:

    For example, if your custom tags/filters are in a file called poll_extras.py, your app layout might look like this:

    polls/
        models.py
        templatetags/
            __init__.py
            poll_extras.py
        views.py
    

    You have defined your templatefilter in views.py. It should be there:

    yourapp/templatetags/__init__.py
    yourapp/templatetags/yourapp_tags.py
    

    First, create yourapp/templatetags/ folder, and yourapp/templatetags/__init__.py empty file. Put your templatetag definition in yourapp_tags.py in that folder.


    And in your template you would use the following:

    {% load poll_extras %}
    

    Finally, in your template, put {% load yourapp_tags %} to enable the templatetag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my helloworld_tags: from django import template register = template.Library() def show_profile(): return {eee: '333'}
my views.py file code: #!/usr/bin/python from django.template import loader, RequestContext from django.http import HttpResponse
This is my views.py: from django.http import HttpResponse from django.template.loader import get_template from django.template
here is my code: urls.py: from django.conf.urls import patterns from views import show,showpage urlpatterns
Django Endless Pagination works perfectly fine right until I use template inheritance. views.py: from
my views.py : from django.core.context_processors import csrf from django.views.decorators.csrf import csrf_protect from django.http import
views.py from django.shortcuts import render_to_response from django.shortcuts import get_object_or_404 from django.shortcuts import get_list_or_404 from
I have the following models: http://slexy.org/view/s20T8yOiKZ from mxutils.cms_services import generate_secid from django.db import models
I am writing a GUI application using Django 1.1.1. This is the views.py: from
I have to change the is_staff option from views.py file to disabling the Django-admin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.