1. Jason Goldstein
  2. scruffy-cropper


Jason Goldstein  committed 3181d62

Post save redirect improvements

  • Participants
  • Parent commits 152165d
  • Branches master

Comments (0)

Files changed (4)

File cropper/templates/cropper/crop.html

View file
             {{ form.as_p }}
         <div class="section controls">
+            <input type="hidden" name="post-save-redirect" value="{{ post_save_redirect }}">
             <input class="button" name="save" type="submit" value="Save Crop" default>
             {% if coordinates %}            

File cropper/views.py

View file
 from django.shortcuts import render, get_object_or_404, redirect
 from django.contrib.contenttypes.models import ContentType
-from django.contrib.contenttypes import generic
 from django.contrib import messages
 from django.db.models.loading import get_model
 from cropper.forms import CropImageForm
-def create_crop(request, app, model_name, object_id, field, template='cropper/crop.html', post_save_redirect=None):
+def create_crop(request, app, model_name, object_id, field, template='cropper/crop.html', post_save_redirect="/"):
     """ Create a crop, or edit an existing one. This is an example of a wrapping view.
     Notice there is no security around this view. I would not call it directly without a wrapper
         crop = Crop.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj), field=field)
         messages.add_message(request, messages.SUCCESS, 'No crop? No problem..')
-        return redirect(post_save_redirect or '/')
+        return redirect(post_save_redirect)
     # Get any existing crop coordinates to pass along
     coordinates = Crop.objects.get_or_create(object_id = obj.id, 
             crop = form.save()
             coordinates = crop.coordinates
             messages.add_message(request, messages.SUCCESS, 'Crop saved! That was easy.')
-            return redirect(post_save_redirect or '/')
+            return redirect(post_save_redirect)
             messages.add_message(request, messages.ERROR, "Not a valid crop.")
     return render(request, template, {
             'form': form,
             'coordinates': coordinates,
+            'post_save_redirect': post_save_redirect,

File cropper/widgets.py

View file
 from django.forms import ClearableFileInput, CheckboxInput
 from django.utils.safestring import mark_safe
+from django.template.defaultfilters import urlencode
 from django.template import Template, Context
 def _snippet(template="", context={}):
     def _crop_button(self, substitutions):
         link = self.attrs.get('data-crop-url')
+        post_save_link = urlencode(self.attrs.get('post-save-redirect') or "")
         if not link:
             return ""
-        return """<a class="button" href="%s">Crop</a>""" % link
+        return """<a class="button" href="%s?post-save-redirect=%s">Crop</a>""" % (link, post_save_link)
     def render(self, name, value, attrs=None):

File setup.py

View file
     name='Scruffy Cropper',
-    version="0.1.2",
+    version="0.1.3",
     author='Jason Goldstein',