Skip to content

Instantly share code, notes, and snippets.

@hvdklauw
Created June 1, 2011 18:46
Show Gist options
  • Save hvdklauw/1002984 to your computer and use it in GitHub Desktop.
Save hvdklauw/1002984 to your computer and use it in GitHub Desktop.
Quick WizardAdmin to get it in the add_view, needs more admin-like-styling for the admin
{% extends 'admin/change_form.html' %}
{% load i18n %}
{% block content %}
<div id="content-main">
<p>Step {{ wizard.steps.current }} of {{ wizard.steps.count }}</p>
<form action="." method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
{% if wizard.steps.prev %}
<button name="wizard_prev_step" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_prev_step" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
</table>
<input type="submit">
</form>
</div>
{% endblock %}
class WizardAdmin(admin.ModelAdmin):
wizard = None
form_list = []
def add_view(self, request, form_url='', extra_context=None):
return self.wizard.as_view(
self.get_form_list(request),
template_name='admin/wizard_form.html')(request, extra_context)
def change_view(self, request, object_id, extra_context=None):
model = self.model
opts = model._meta
obj = self.get_object(request, unquote(object_id))
if not self.has_change_permission(request, obj):
raise PermissionDenied
if obj is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)})
instance_dict = {}
for i in range(len(self.form_list)):
instance_dict.update({str(i): obj})
context = {
'title': _('Change %s') % force_unicode(opts.verbose_name),
'object_id': object_id,
'original': obj,
'is_popup': "_popup" in request.REQUEST,
'root_path': self.admin_site.root_path,
'app_label': opts.app_label,
'opts': opts,
}
context.update(extra_context or {})
return self.wizard.as_view(
self.get_form_list(request, obj),
template_name='admin/wizard_form.html', instance_dict=instance_dict, extra_context=context)(request)
def get_form_list(self, request, obj=None, **kwargs):
list = []
for form in self.form_list:
self.form = form
list.append(self.get_form(request, obj, **kwargs))
return list
Index: contrib/formtools/wizard/views.py
===================================================================
--- contrib/formtools/wizard/views.py (revision 16308)
+++ contrib/formtools/wizard/views.py (working copy)
@@ -96,6 +96,7 @@
instance_dict = None
condition_dict = None
template_name = 'formtools/wizard/wizard_form.html'
+ extra_context = None
def __repr__(self):
return '<%s: forms: %s>' % (self.__class__.__name__, self.form_list)
@@ -112,7 +113,7 @@
@classmethod
def get_initkwargs(cls, form_list,
- initial_dict=None, instance_dict=None, condition_dict=None):
+ initial_dict=None, instance_dict=None, condition_dict=None, template_name=None, extra_context=None):
"""
Creates a dict with all needed parameters for the form wizard instances.
@@ -168,6 +169,8 @@
# build the kwargs for the formwizard instances
kwargs['form_list'] = init_form_list
+ kwargs['template_name'] = template_name
+ kwargs['extra_context'] = extra_context
return kwargs
def get_wizard_name(self):
@@ -379,7 +382,7 @@
# If the form is based on ModelFormSet, add queryset if available.
kwargs.update({'queryset': self.get_form_instance(step)})
return self.form_list[step](**kwargs)
-
+
def process_step(self, form):
"""
This method is used to postprocess the form data. By default, it
@@ -513,11 +516,12 @@
return context
"""
context = super(WizardView, self).get_context_data(*args, **kwargs)
+ context.update(self.extra_context or {})
context.update(self.storage.extra_data)
context['wizard'] = {
'form': form,
'steps': self.steps,
- 'managenent_form': ManagementForm(prefix=self.prefix, initial={
+ 'management_form': ManagementForm(prefix=self.prefix, initial={
'current_step': self.steps.current,
}),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment