12

I have a end_date field on my news apps which is a facultative field. When a author is editing one of his new and erase the end_date, I set in the model the end_date to None.

My problem is that the field then display the value 'None' on next edit instead of just being blank.

Here is my field :

models.py

end_date = models.DateTimeField(null=True, blank=True)

forms.py

end_date = forms.CharField(label='', required=False, widget=forms.DateTimeInput(attrs={'class': 'form-control', 'format': 'DD, d MM yy', 'placeholder': _('End date')}))

and the assignation in my view :

views.py

 if news_form.cleaned_data['end_date'] == '' :
    edited_new.end_date = None 
 edited_new.save()

EDIT :

here is my template :

news.html

        <form action="#" method="post" role="form" id="news-form">{% csrf_token %}
        {{ news_form.media }}
        <div class="col-xs-12">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-xs-12 col-md-6">
                            <h2 class="panel-title">{% trans "Add or edit news" %}</h2>
                        </div>
                        <div class="col-xs-12 col-md-6">
                            <div class="pull-right">
                                <a href="#" class="btn btn-secondary cancel"><span class="glyphicon glyphicon-floppy-remove"></span> {% trans "Cancel" %}</a>
                                <button type="submit" class="btn btn-primary form-btn save"><span class="glyphicon glyphicon-floppy-saved"></span><span class="button-text"> {% trans "Save" %}</span></button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-xs-12 col-md-6">
                            {{ news_form.title }}
                        </div>
                        <div class="col-xs-4 col-md-2">
                            {% trans 'Tag' %}
                        </div>
                        <div class="col-xs-8 col-md-4">
                            {{ news_form.tag }}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 no-label-field redactor-field">
                            {{ news_form.message }}
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-6 date-field no-label-field">
                            <div class="form-group">
                                <div class='input-group date datetimepicker'>
                                    {{ news_form.start_date }}
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-6 date-field no-label-field">
                            <div class="form-group">
                                <div class='input-group date datetimepicker'>
                                    {{ news_form.end_date }}
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12">
                            {{ news_form.active }} {% trans 'Activate the news' %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <span style="display:none;">{{ news_form.id }}</span>
    </form>
0

1 Answer 1

31

You can use the filter default_if_none available since Django 1.3, it'll only work for values that are None.

{{ news_form.end_date|default_if_none:'' }}

Docs

Sign up to request clarification or add additional context in comments.

3 Comments

It only works on value, not on form field, is it not ?
It only works on values: {{ news_form.end_date.value|default_if_none:'' }} Also it dosnt work with blank value. Only works if there is something inside' ' {{ news_form.end_date.value|default_if_none:'a' }} This will work
Why is the default for 'None' not '' (empty)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.