The Modify Function

The Modify view function is very similar to that of the Add function, except that instead of creating an empty form for the initial view, it retrieves existing data and displays it in the form. So in Listing 4-3 we first search for an existing DHCP network object and pass it to the form class.

Listing 4-3. The view to handle the Modify function for DHCP networks def modify_dhcp(request, address=None): ip, net_size = address.split('/')

network_addr = NetworkAddress.objects.get(address=ip, network_size=int(net_size))

dhcp_net = DHCPNetwork.objects.get(physical_net=network_addr) if request.method == 'POST':

# submiting changes form = DHCPNetworkAddForm(request.POST, instance=dhcp_net) if form.is_valid():

return HttpResponseRedirect("../..")


# first time display form = DHCPNetworkAddForm(instance=dhcp_net) return render_to_response('add.html', {'form': form,})

Was this article helpful?

0 0

Post a comment