Skip to content

Commit

Permalink
Fixed Values are not retained in forms (event, job and shift) #742
Browse files Browse the repository at this point in the history
Modified post view of forms to retain the values of submitted form
when there is an error in form validation to increase users experience.

Fixes #742
  • Loading branch information
codesankalp committed Sep 28, 2020
1 parent f8a4ea7 commit 9b7d6c7
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions vms/event/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,40 @@ def post(self, request, *args, **kwargs):
return HttpResponseRedirect(reverse('event:list'))
else:
data = request.POST.copy()
try:
country_name = data.get('country', '')
country = Country.objects.get(name=country_name)
except ObjectDoesNotExist:
country = None
try:
state_name = data.get('state', '')
state = Region.objects.get(name=state_name)
except ObjectDoesNotExist:
state = None
try:
city_name = data.get('city', '')
city = City.objects.get(name=city_name)
except ObjectDoesNotExist:
city = None
for field in data.keys():
data[field] = data.get(field, '')
try:
data['start_date'] = form.cleaned_data['start_date']
except KeyError:
data['start_date'] = ''
try:
data['end_date'] = form.cleaned_data['end_date']
except KeyError:
data['end_date'] = ''
state_list = Region.objects.filter(country=country)
city_list = City.objects.filter(region=state)
form = EventForm(data)
return render(request, 'event/edit.html', {
'form': form,
'country_list': Country.objects.all(),
'state_list': state_list,
'city_list': city_list,
'event': event
})


Expand Down

0 comments on commit 9b7d6c7

Please sign in to comment.