Django

Code

Ticket #3204 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] Need ability to pass a RequestContext instance to FormPreview.

Reported by: Vadim Macagon <vadim@cubik.org> Assigned to: adrian
Milestone: Component: django.contrib.formtools
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

I tried using the FormPreview? class in django.contrib.formtools.preview today. But my templates rely on a RequestContext? as opposed to just the plain Context produced by all the render_to_response() calls used by FormPreview?.

Here's what I'm currently doing:

# urls.py

(r'^apples/add/$', 'apples.views.add_apple')


# views.py

class ApplePreview(FormPreview):
    preview_template = 'apples/preview.html'
    form_template = 'apples/form.html'
    
    def done(self, request, clean_data):
        # create apple object and save to db
        # ...
        return HttpResponseRedirect('/')

@login_required
@transaction.commit_on_success
def add_apple(request):
    # ARGH! Can't pass in a RequestContext!
    return ApplePreview(AppleForm)(request)

So I suggest modifying FormPreview? slightly so that the initialization method takes an optional context_instance arg that can be passed to all the render_to_response calls in preview_get(), preview_post() and post_post(). I've attached a diff with the suggested changes. With these changes I can write this:

@login_required
@transaction.commit_on_success
def add_apple(request):
    # Yay!
    return ApplePreview(AppleForm, RequestContext(request))(request)

Attachments

formpreview.diff (2.8 kB) - added by Vadim Macagon <vadim@cubik.org> on 12/29/06 01:50:09.

Change History

12/29/06 01:50:09 changed by Vadim Macagon <vadim@cubik.org>

  • attachment formpreview.diff added.

12/29/06 02:04:19 changed by Vadim Macagon <vadim@cubik.org>

  • summary changed from Need ability to pass a RequestContext instance to FormPreview. to [patch] Need ability to pass a RequestContext instance to FormPreview..

12/29/06 13:39:37 changed by adrian

If we allow for a context_instance argument, as this patch provides, then it becomes necessary to wrap the FormPreview class within a view. It's intended to be used directly *as* a view rather than being wrapped.

A better way of adding the functionality, then, would just be to use RequestContext by default.

12/29/06 13:42:56 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [4259]) Fixed #3204 -- Changed FormPreview? to use RequestContext?


Add/Change #3204 ([patch] Need ability to pass a RequestContext instance to FormPreview.)




Change Properties
Action