Advertisement
DigitalMag

Forms without helptext

Jan 14th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. class CreateDivHTML(CreateHTML):    
  2.     """
  3.     Делает help_text_html невидимым и добавляет к нему класс help-text
  4.     """
  5.     def as_h(self):
  6.        
  7.         as_p = self._html_output(
  8.             normal_row = u'<div%(html_class_attr)s>%(label)s %(field)s %(help_text)s %(errors)s</div>',
  9.             error_row = u'<div class="error">%s</div>',
  10.             row_ender = '</div>',
  11.             help_text_html = u'<div hidden class="help-text">%s</div>',
  12.             errors_on_separate_row = False)
  13.        
  14.         csrf_t = '<p style="color:red">Set csrf in your view</p>'
  15.         submit = self.submit
  16.         cssclass = format_html(u'class="{}"', self.css_class) if len(self.css_class) > 0 else ''
  17.        
  18.         if self.request != None:
  19.             csrf_t = '<input type="hidden" name="csrfmiddlewaretoken" value="' + get_token(self.request) + '">'
  20.         if self.submit == 'def':
  21.             submit = 'in_' + self.__class__.__name__ + '_notdefined'
  22.            
  23.         html = '<form method="post" '+ cssclass + '>' + csrf_t + as_p + '<input type="submit" value=' + submit + '></form>'
  24.        
  25.         return mark_safe(html)
  26.  
  27.  
  28. class SignUpForm(UserCreationForm, CreateDivHTML):
  29.     email = forms.EmailField(required=True, label='Email')
  30.  
  31.     class Meta:
  32.         model = Profile
  33.         fields = ("username", "email", "password1", "password2")
  34.  
  35.     def __init__(self, *args, **kwargs):        
  36.         super(SignUpForm, self).__init__(*args, **kwargs)
  37.         self.submit = u'Присоединиться'
  38.        
  39.     def save(self, commit=True):
  40.         user = super(SignUpForm, self).save(commit=False)
  41.         user.email = self.cleaned_data["email"]
  42.         if commit:
  43.             user.save()
  44.         return user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement