Created
August 22, 2021 09:37
-
-
Save skolo-online/be4ceef7dfe487dabb0648c08d5c9f13 to your computer and use it in GitHub Desktop.
Forms - Extend init function with crispy forms layouts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.forms import widgets | |
from .models import * | |
#Form Layout from Crispy Forms | |
from crispy_forms.helper import FormHelper | |
from crispy_forms.layout import Layout, Submit, Row, Column | |
class DateInput(forms.DateInput): | |
input_type = 'date' | |
class InvoiceForm(forms.ModelForm): | |
THE_OPTIONS = [ | |
('14 days', '14 days'), | |
('30 days', '30 days'), | |
('60 days', '60 days'), | |
] | |
STATUS_OPTIONS = [ | |
('CURRENT', 'CURRENT'), | |
('OVERDUE', 'OVERDUE'), | |
('PAID', 'PAID'), | |
] | |
title = forms.CharField( | |
required = True, | |
label='Invoice Name or Title', | |
widget=forms.TextInput(attrs={'class': 'form-control mb-3', 'placeholder': 'Enter Invoice Title'}),) | |
paymentTerms = forms.ChoiceField( | |
choices = THE_OPTIONS, | |
required = True, | |
label='Select Payment Terms', | |
widget=forms.Select(attrs={'class': 'form-control mb-3'}),) | |
status = forms.ChoiceField( | |
choices = STATUS_OPTIONS, | |
required = True, | |
label='Change Invoice Status', | |
widget=forms.Select(attrs={'class': 'form-control mb-3'}),) | |
notes = forms.CharField( | |
required = True, | |
label='Enter any notes for the client', | |
widget=forms.Textarea(attrs={'class': 'form-control mb-3'})) | |
dueDate = forms.DateField( | |
required = True, | |
label='Invoice Due', | |
widget=DateInput(attrs={'class': 'form-control mb-3'}),) | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.helper = FormHelper() | |
self.helper.layout = Layout( | |
Row( | |
Column('title', css_class='form-group col-md-6'), | |
Column('dueDate', css_class='form-group col-md-6'), | |
css_class='form-row'), | |
Row( | |
Column('paymentTerms', css_class='form-group col-md-6'), | |
Column('status', css_class='form-group col-md-6'), | |
css_class='form-row'), | |
'notes', | |
Submit('submit', ' EDIT INVOICE ')) | |
class Meta: | |
model = Invoice | |
fields = ['title', 'dueDate', 'paymentTerms', 'status', 'notes'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment