Deleting Shapefiles

The final piece of functionality we'll need to implement is the "delete shapefile" view. This will let the user delete an entire uploaded Shapefile. The process is basically the same as for deleting features; we've already got a Delete hyperlink on the main page, so all we have to do is implement the underlying view.

Edit urls.py and add the following entry to the geodjango.shapeEditor.views URL pattern list:

  (r'^shape-editor/delete/(?P<shapefile_id>d+)$',
   'deleteShapefile'),

Then edit views.py and add the following new view function:

def deleteShapefile(request, shapefile_id):
  try:
    shapefile = Shapefile.objects.get(id=shapefile_id)
  except Shapefile.DoesNotExist:
    raise Http404

  if request.method == "GET":
    return render_to_response("deleteShapefile.html",
                              {'shapefile' : shapefile})
  elif request.method == "POST":
    if request.POST['confirm'] == "1":
      shapefile.delete()
    return HttpResponseRedirect("/shape-editor")

Notice that we're passing the Shapefile object to the template. This is because we want to display some information about the Shapefile on the confirmation page.

Note

Remember that shapefile.delete()doesn't just delete the Shapefile object itself; it also deletes all the objects associated with the Shapefile through ForeignKey fields. This means that the one call to shapefile.delete() will also delete all the Attribute, Feature, and AttributeValue objects associated with that Shapefile.

Finally, create a new template named deleteShapefile.html, and enter the following text into this file:

<html>
  <head>
    <title>ShapeEditor</title>
  </head>
  <body>
    <h1>Delete Shapefile</h1>
    <form method="POST">
      Are you sure you want to delete the
      "{{ shapefile.filename }}" shapefile?
      <p/>
      <button type="submit" name="confirm"
              value="1">Delete</button>
      &nbsp;
      <button type="submit" name="confirm"
              value="0">Cancel</button>
    </form>
  </body>
</html>

You should now be able to click on the Delete hyperlink to delete a Shapefile. Go ahead and try it; you can always re-import your Shapefile if you need it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.145