django forms error_class -


Is there a way to provide a specific error in the function in fire defragment? Under the docs, it shows that you can give a form a special error rendering function, but it seems like you have to declare when you have the form, not when you have an illustration of defining it.

You can define something like ErrorList class:

  Import ErrorList class DivErrorList (ErrorList) from django.forms.util: def __unicode __ (self): Return self.as_divs () DEF as_divs (self): Otherwise self: return you 'U' return U '& lt; Div class = "errorlist" & gt;% s & lt; / Div> % '' .join ([U '
< P> And then when you have an explanation of your form, you can illustrate this with error_class:

  f = ContactForm (data, auto_id = false, error_class = DivErrorList) f.as_p ( ) & Lt; Div class = "errorlist" & gt; & Lt; Div class = "error" & gt; This field is required. & Lt; / Div & gt; & Lt; / Div & gt; & Lt; P & gt; Subject: & lt; Input type = "text" name = "topic" maximum length = "100" /> < / P & gt; & Lt; P & gt; Message: & lt; Input type = "text" name = "message" value = "hello there" /> & lt; / P & gt; & Lt; Div class = "errorlist" & gt; & Lt; Div class = "error" & gt; Enter a valid e-mail address. & Lt; / Div & gt; & Lt; / Div & gt; & Lt; P & gt; From: & lt; Input type = "text" name = "sender" value = "invalid e-mail address" /> gt; & Lt; / P & gt; & Lt; P & gt; CC itself: & lt; Input checked = "checked" type = "checkbox" name = "cc_myself" /> gt; & Lt; / P & gt;  

But every time I use a form, I do not want to name the error class, is there a way to define a custom error renderer within the form definition?

If you want this behavior to be normal in all your forms, then you have your own form base Class can be defined like:

  class MyBaseForm (forms.Form): def __init __ (auto, * args, ** kwargs): kwargs_new = {'error_class': DivErrorList} kwargs_new .update (kwargs) super (MyBaseForm, self) .__ init __ (self, * args, kwargs_new)  

And then one that is all your subcategory form. Then all of your form will have DivErrorList as a default error renderer, and you will still be able to change it using the error_class argument.

For ModelForm:

  class MyBaseModelForm (forms.ModelForm): def __init __ (auto, * args, ** kwargs): kwargs_new = {'error_class': DivErrorList} kwargs_new.update (kwargs) super (MyBaseModelForm, itself) .__ init __ (* args, ** kwargs_new)  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -