Example #1
0
def modelUpdate(request):	
	print "Creation page"
	m_tb_name = request.session['model_table']
	print 'model_classes_field', m_tb_name
	model_class = get_model('Directories', m_tb_name)
	model_name = model_class.__name__
	field_names = model_class._meta.get_all_field_names()			
	if request.method == 'POST': 
		print "m_tb_name is: ", m_tb_name
		form_class = get_dynamic_form(m_tb_name)
		form = form_class(request.POST)			
		if form.is_valid(): # All validation rules pass
			print "form is valid!" 
			row = form.save() 
			form = dbForm()
			#send a message to inform users that form submission was a success
			messages.success(request, 'New model data created!')
			return HttpResponseRedirect(reverse('Directories:index'))
		else:
			#will probably return the form with errors
			return render(request, 'Directories/create.html', {'form':form, 'model_name':model_name, 'field_names':field_names})
		
	else:
		form_class = get_dynamic_form(m_tb_name)
		form = form_class() # An unbound form
		print "no form submission: ", form.errors
	return render(request, 'Directories/create.html', {'form':form, 'model_name':model_name, 'field_names':field_names})
Example #2
0
def index(request): #for two submit buttons:
	print "index"
	clean(request) # clean all data that may have been previously added
	form = dbForm()
	# check if form has been submitted since user may have returned back to form page
	if request.method == 'GET':	
		print "get request"
		if "_change" in request.GET:
			form = dbForm(request.GET) 
			print "i am in _change submit button"
			if form.is_valid(): # All validation rules pass
				print "bound form, get data"
				model_classes_field = form.cleaned_data['model_classes_field']
				request.session['model_table'] = model_classes_field	
				print "success"
				return HttpResponseRedirect(reverse('Directories:list_models'))		
			else:
				#will return form errors
				return render(request, 'Directories/index.html', {'form':form})
		elif '_add' in request.GET:
			form = dbForm(request.GET)
			print "i am in _add submit button"
			if form.is_valid(): # All validation rules pass
				print "bound form, get data"
				model_classes_field = form.cleaned_data['model_classes_field']
				request.session['model_table'] = model_classes_field	
				return HttpResponseRedirect(reverse('Directories:update_directories'))	
			else:
				#will return form errors
				return render(request, 'Directories/index.html', {'form':form})
		
		else:
			print "submit button not pressed."
	else:
		form = dbForm()
		print "no POST - form: ", form.errors
		print "unbound form"
 	return render(request, 'Directories/index.html', {'form':form})