Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django widget tweaks 'render_field' not working #60

Open
tkhurana96 opened this issue Jan 18, 2018 · 5 comments
Open

Django widget tweaks 'render_field' not working #60

tkhurana96 opened this issue Jan 18, 2018 · 5 comments
Labels

Comments

@tkhurana96
Copy link

I have this form rendering template in django, and render_field is not working inside if-else:

{% load widget_tweaks %} 
{% for field in form %}
    <div class="form-group">
        {{ field.label_tag }} 
        {% if form.is_bound %} 
            {% if field.errors %} 
                {% render_field field class="form-control is-invalid" %} 
                {% for error in field.errors %}
                    <div class="invalid-feedback">
                        {{error}}
                    </div>
                {% endfor %} 
            {% else %} 
                {% render_field field class="form-control is-valid" %} 
            {% endif %} 
        {% else %} 
            {% render_field field class="form-control" %} 
        {% endif %} 

        {% if field.help_text %}
            <small class="form-text text-muted">
               {{ field.help_text }}
            </small>
        {% endif %}
    </div>
{% endfor %}

And it renders this:

But when I try a smaller version of the above code like following, then also it still doesn't work:

{% load widget_tweaks %} 
{% for field in form %}
        <div class="form-group">

            {% if field.errors %}
                {% render_field field class="form-control is-invalid" %}
            {% else %}
                {% render_field field class="form-control is-valid" %}
            {% endif %}

            {% if field.help_text %}
                <small class="form-text text-muted">
                    {{ field.help_text }}
                </small>
            {% endif %}

        </div>
{% endfor %}

And renders this:

But when I remove all if-else's:

{% load widget_tweaks %} 
{% for field in form %}
    <div class="form-group">
        {{ field.label_tag }} 
        {% render_field field class="form-control" %} 
        {% if field.help_text %}
            <small class="form-text text-muted">
               {{ field.help_text }}
            </small>
        {% endif %}
    </div>
{% endfor %}

Then it renders correctly:

But I need the if-else's to check for form errors and add error messages to form accordingly, will appreciate any help. So why is render_field not working with if-else ? How to resolve this ? Any other ways to use widget_tweaks to render error ?

@DorMiX
Copy link

DorMiX commented Feb 28, 2018

Unfortunately, the last code snippet does not work with Django2.0.2 at all. :(

@makr11
Copy link

makr11 commented Mar 3, 2018

@camilonova
Copy link
Member

Please try again using the latest release

@Cataldir
Copy link

Just tried with latest django-widgets-improved (1.5.0) with

System check identified no issues (0 silenced). September 29, 2018 - 17:48:27 Django version 2.0.7, using settings 'cataldi.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.

Didn't work either:

image

@zodman
Copy link
Member

zodman commented May 24, 2019

use add_error_class instead of if-else

https://github.com/jazzband/django-widget-tweaks#add_error_class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants