Giter Site home page Giter Site logo

Comments (3)

gregmuellegger avatar gregmuellegger commented on May 30, 2024

The reason seems to be a bug (or at least an unexpected behaviour on my side) in django. It is handling invalid values differently when they are assigend to a new value via "with", which makes them truthy if TEMPLATE_STRING_IF_INVALID is set:

In [16]: Template('{% with not_exist as value %}{% if value %}Hello {{ value }}{% endif %}{% endwith %}').render(Context())
Out[16]: u'Hello <INVALID>'

You can fix this in your project with a quick workaround. Put this in your settings.py:

class Invalid(unicode):
    def __nonzero__(self):
        return False

TEMPLATE_STRING_IF_INVALID = Invalid('<INVALID>')

However this will still break the textarea widget with floppyforms (as a quick test showed for me). But this should have been the same behaviour in older floppyforms versions. To fix this, create in your project a floppyforms/textarea.html file with the following content:

<textarea name="{{ name }}"{% if required %} required{% endif %}{% include "floppyforms/attrs.html" %}>{{ value|default:"" }}</textarea>

from django-floppyforms.

gregmuellegger avatar gregmuellegger commented on May 30, 2024

Django's behaviour is even documented here:
https://docs.djangoproject.com/en/dev/ref/templates/api/#invalid-template-variables

We provide documentation on that in floppyforms and the fix for the textarea.html template shortly.

from django-floppyforms.

leon-matthews avatar leon-matthews commented on May 30, 2024

Thanks very much Gregor. Your help is very much appreciated!

I also found the behaviour of the 'with' tag in your explanation surprising -- although the question of what the 'with' tag should do in light of missing values is a difficult one when exceptions are verboten...

I had read the advice in the Django Docs about not using a value for TEMPLATE_STRING_IF_INVALID as a development default, but had understood it just as a backwards compatibility issue -- recent versions of the admin templates no longer require an empty value to work.

from django-floppyforms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.