class ProfileForm(forms.Form): choices = (('N', 'No Thanks'), ('D', 'Daily'), ('W', 'Weekly'), ('M', 'Monthly')) avatar = ImageFormField(required=False) name = forms.CharField(max_length=50, required=False) location = forms.CharField(max_length=20, required=False) about = forms.CharField(widget=forms.Textarea(), max_length=1000, required=False) subscription = forms.ChoiceField(choices=choices, label='Subscribe to Updates') def __init__(self, *args, **kwargs): super(ProfileForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.layout = Layout( Div(HTML( """<div class='avatar'>{% load thumbnail %}{{ form.avatar }}{% if form.avatar.value %}{% thumbnail form.avatar.value "300x200" as im %}<img src='{{ im.url }}{% endthumbnail %}'>{% endif %}></div>""" ), css_class='pure-u-1 pure-u-md-6-12 pure-u-lg-5-12 ls'), Div(Field('name'), Field('location'), Field('about'), Field('subscription'), Hidden('username', value=self.initial['username']), css_class='pure-u-1 pure-u-md-6-12 pure-u-lg-7-12 rs'), Submit(name='save', value='Save', css_class='pure-button'), )
def __new__(cls, *args, **kwargs): """ Add sections dynamically :param cls: :param args: :param kwargs: :return: """ new_class = super(BlogEntryUpdateForm, cls).__new__(cls, *args, **kwargs) article = kwargs.get('initial') base_fields = {} for section in article.articlesection_set.all(): delim = '' if section.section_order == 1 else '_' + str(section.section_order) base_fields['content' + delim] = forms.CharField(widget=CKEditorWidget(), initial=section.content) base_fields['score' + delim] = forms.DecimalField(max_value=settings.RATING_SCALE, max_digits=settings.RATING_MAX_DIGITS, min_value=0, initial=section.score) base_fields['id' + delim] = forms.IntegerField(initial=section.id) if section.section_order == 1: base_fields['title' + delim] = forms.CharField(max_length=150, initial=section.title) base_fields['title'].widget.attrs['readonly'] = 'readonly' base_fields['image'] = ImageFormField(initial=article.image) base_fields['article'] = forms.IntegerField(initial=article.id) base_fields['tags' + delim] = TagField(initial=', '.join(article.tags.names())) else: base_fields['title' + delim] = forms.CharField(max_length=150, initial=section.title) new_class.base_fields = base_fields return new_class
class BlogEntryForm(forms.Form): title = forms.CharField(max_length=150) image = ImageFormField() tags = TagField() content = forms.CharField(widget=CKEditorWidget()) score = forms.DecimalField(max_value=settings.RATING_SCALE, max_digits=settings.RATING_MAX_DIGITS, min_value=0) def __init__(self, *args, **kwargs): super(BlogEntryForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_class = 'pure-form pure-form-stacked pure-u-1' self.helper.layout = Layout( Div( Button('reset', 'Reset Section', data_section_id=1, data_btn_nm='reset', wrapper_class='rep', css_class='sec_btn pure-button'), Button('del', 'Delete Section', data_section_id=1, data_btn_nm='del', wrapper_class='rep', css_class='sec_btn pure-button'), Field('title', wrapper_class='rep pure-control-group', css_class='rep'), # Class rep tells it is replicable item Field('score', wrapper_class='rep pure-control-group', css_class='rep'), # Class rep tells it is replicable item Field('image', wrapper_class='norep pure-control-group', css_class='rep'), # Class norep tells the item must be removed from replica's Field('content', wrapper_class='rep pure-control-group', css_class='rep'), css_class='section', css_id='sec', data_section_id=1 ), Field('tags', wrapper_class='norep pure-control-group', css_class='norep'), ButtonHolder( Button('add', 'Add Section', data_btn_nm='add', data_section_count=1, css_id='add', css_class='sec_btn pure-button'), Button('save', 'Save Section', css_id='save', css_class='pure-button'), Submit('submit', 'Submit Post', css_id='submit', css_class='pure-button pure-button-primary'), css_id='buttons' ), ) class Media: js = ( )
def __init__(self, max_length=None, **kwargs): fields = (ImageFormField(max_length=max_length, **kwargs), forms.BooleanField()) super(ClearableImageFormField, self).__init__(fields, **kwargs)