python - pop up dialog box instead raising form errors django -
this error message wish replaced pop dialog box
right code showing error message example "studentid: ensure value has @ least 8 characters" in page before field when studentid has less or more 8 characters, start , end date have no difference of 14 days, , when studentid , startdate not unique. able pop dialog box when student id, student name , checkbox not filled. how pop dialog box being shown when start , end date field empty, start , end date has no difference of 14 days, studentid , startdate not unique, studentid has less/more 8 characters ?
views.py
def timesheet(request): if request.method == "post": form = timesheetform(request.post) if form.is_valid(): timesheet = form.save(commit=false) timesheet.save() else: form = timesheetform() return render(request, 'hrfinance/timesheet.html', {'form': form}) class timesheetform(forms.modelform): checkbox = forms.booleanfield() studentname = forms.charfield() startdate = forms.datefield() enddate = forms.datefield() class meta: model = timesheet fields = '__all__' def clean(self): cleaned_data = super(timesheetform, self).clean() startdate = cleaned_data.get("startdate") enddate = cleaned_data.get("enddate") if startdate , enddate: if enddate < startdate: raise forms.validationerror("end date cannot earlier start date!") dt = enddate - startdate if not dt.days == 14: raise forms.validationerror("duration must 2 weeks!") return cleaned_data
models.py
class timesheet(models.model): studentid = models.charfield("student id", max_length=8, validators=[minlengthvalidator(8)], default="", unique_for_date="startdate") studentname = models.charfield("student name", max_length=500, default="") startdate = models.datefield("startdate", blank=true) enddate = models.datefield("enddate", blank=true) def __str__(self): return self.studentid
Comments
Post a Comment