开发者

Change the value of a form using BoundField in Django?

开发者 https://www.devze.com 2023-04-12 12:16 出处:网络
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 i

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号