[Answer]-How to use messages in Django?

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
👤ruddra

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 !

👤Mazdak

Leave a comment