I am trying to use a custom template filter to change the value of a form and spit it back out. Specifically, I am trying to do what the cut
template filter already does, but using lstrip
and rstrip
instead.
from django.template import Library
register = Library()
def lstrip(value, arg):
return value.value() #I can get the form value, but how do I change it?
register.filter('开发者_如何学编程lstrip',lstrip)
In my template:
#import custom template tags
{{ form.url|lstrip:"/" }}
How do I change the field value and return the field with the new value in place (just like the cut
filter already does)?
first, I would change the name of your template tag, because lstrip is a built-in method of python. In your renamed template tag, you can actually use lstrip if that's what you want to do:
def my_custom_strip(value, arg):
return value.lstrip(arg)
and in your template
{{ form.url|my_custom_strip:"/" }}
Hope this helps,
Hoff
精彩评论