0

Below, I have this div that is responsible to show any error, warning and success message. But I figure out how to use it with the success case only.

This is my views.py

def registration(request):
    form = PersonForm(request.POST or None)

    if form.is_valid():
        save_it = form.save(commit=False)
        save_it.save()
        messages.success(request, 'Your form was saved') 

     <if checkbox not checked>  <<< is this possible ?
       messages.error(request, 'You must accept the terms to register')

    return render(request, 'provisioning/registration.html', {'form':form,})

And this is my registration.py

    <div>
        ...
        {% if messages %}
            {% for message in messages %}
            <div {% if message.tags %} class="alert alert-{{ message.tags }}" {% endif %} alert-danger fade in">
                <button data-dismiss="alert" class="close close-sm" type="button">
                    <i class="fa fa-times"></i>
                </button>
                <strong>{{ message.tags }} | </strong> {{message}}
            </div>
            {% endfor %}
        {% endif %}
    </div>

My question is: how can I validate my fields and show if everything went fine or not in my html page ?

For example, there is a checkbox for the agreement of terms of use, right ? How can I use the messages to tell to the user that he need to accept the terms of use to register in case if wasn't checked by the user.

2 Answers 2

1

How about using clean method?

def clean_agreement(self):
        data = self.cleaned_data
        if not data['agreement']:
            msg = u"You have to agree with the terms and conditions."
            self._errors["agreement"] = self.error_class([msg])
            return False
        return True
Sign up to request clarification or add additional context in comments.

Comments

0

Why you don't use JavaScript for show your messages ? its very elegant and have more performance ! you can use a function like this :

var showMessage = function(element, msg, where) {
    var div = $('<div class="your_classname"><h3>' +msg+ '</h3>(' +
    gettext('click to close') + ')</div>');
    div.click(function(event) {
        $(".your_class_name").fadeOut("fast", function() { $(this).remove(); });
    });

    var where = where || 'parent';

    if (where == 'parent'){
        element.parent().append(div);
    }
    else {
        element.after(div);
    }

    div.fadeIn("fast");
};

but about your question for validating forms read THIS good recipe ! also you can use THIS ONE from Django tutorial !

4 Comments

Sorry, but I didn't understand how these links are related to my question.
because i read this " how can I validate my fields and show if everything went fine or not in my html page"
I don't want to use JavaScript right now, to don't mix the things with Django, because I'm focused to learn about Python + Django. And I believe that should have a solution using Django itself.
Got it, I will update my question then to be more specific about it.

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.