class NamedUrlWizardView
from django.contrib.formtools.wizard.views import NamedUrlWizardView
A WizardView with URL named steps support.
Ancestors (MRO)
- NamedUrlWizardView
- WizardView
- TemplateView
- TemplateResponseMixin
- ContextMixin
- View
Descendants
Attributes
Defined in | |
---|---|
condition_dict = None
|
WizardView |
content_type = None
|
TemplateResponseMixin |
done_step_name = None
|
NamedUrlWizardView |
form_list = None
|
WizardView |
http_method_names = [u'get', u'post', u'put', u'patch', u'delete', u'head', u'options', u'trace']
|
View |
initial_dict = None
|
WizardView |
instance_dict = None
|
WizardView |
response_class = <class 'django.template.response.TemplateResponse'>
|
TemplateResponseMixin |
storage_name = None
|
WizardView |
template_name = 'formtools/wizard/wizard_form.html'
|
WizardView |
template_name = None
|
TemplateResponseMixin |
url_name = None
|
NamedUrlWizardView |
Methods
def
__init__(self, **kwargs):
View
¶
def
__init__(self, **kwargs):
View
¶
Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.
36 37 38 39 40 41 42 43 44 | def __init__(self, **kwargs):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
for key, value in six.iteritems(kwargs):
setattr(self, key, value)
|
def
__repr__(self):
WizardView
¶
def
__repr__(self):
WizardView
¶
111 112 | def __repr__(self):
return '<%s: forms: %s>' % (self.__class__.__name__, self.form_list)
|
def
_allowed_methods(self):
View
¶
def
_allowed_methods(self):
View
¶
107 108 | def _allowed_methods(self):
return [m.upper() for m in self.http_method_names if hasattr(self, m)]
|
def
as_view(cls, *args, **kwargs):
¶
def
as_view(cls, *args, **kwargs):
¶
WizardView
This method is used within urls.py to create unique wizardview instances for every request. We need to override this method because we add some kwargs which are needed to make the wizardview usable.
114 115 116 117 118 119 120 121 122 | @classonlymethod
def as_view(cls, *args, **kwargs):
"""
This method is used within urls.py to create unique wizardview
instances for every request. We need to override this method because
we add some kwargs which are needed to make the wizardview usable.
"""
initkwargs = cls.get_initkwargs(*args, **kwargs)
return super(WizardView, cls).as_view(**initkwargs)
|
View
Main entry point for a request-response process.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | @classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
# sanitize keyword arguments
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key))
def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
# take name and docstring from class
update_wrapper(view, cls, updated=())
# and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
return view
|
def
dispatch(self, request, *args, **kwargs):
¶
def
dispatch(self, request, *args, **kwargs):
¶
WizardView
This method gets called by the routing engine. The first argument is `request` which contains a `HttpRequest` instance. The request is stored in `self.request` for later use. The storage instance is stored in `self.storage`. After processing the request using the `dispatch` method, the response gets updated by the storage engine (for example add cookies).
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | def dispatch(self, request, *args, **kwargs):
"""
This method gets called by the routing engine. The first argument is
`request` which contains a `HttpRequest` instance.
The request is stored in `self.request` for later use. The storage
instance is stored in `self.storage`.
After processing the request using the `dispatch` method, the
response gets updated by the storage engine (for example add cookies).
"""
# add the storage engine to the current wizardview instance
self.prefix = self.get_prefix(*args, **kwargs)
self.storage = get_storage(self.storage_name, self.prefix, request,
getattr(self, 'file_storage', None))
self.steps = StepsHelper(self)
response = super(WizardView, self).dispatch(request, *args, **kwargs)
# update the response (e.g. adding cookies)
self.storage.update_response(response)
return response
|
View
79 80 81 82 83 84 85 86 87 | def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)
|
def
done(self, form_list, **kwargs):
WizardView
¶
def
done(self, form_list, **kwargs):
WizardView
¶
This method must be overridden by a subclass to process to form data after processing all steps.
573 574 575 576 577 578 579 | def done(self, form_list, **kwargs):
"""
This method must be overridden by a subclass to process to form data
after processing all steps.
"""
raise NotImplementedError("Your %s class has not defined a done() "
"method, which is required." % self.__class__.__name__)
|
def
get(self, *args, **kwargs):
¶
def
get(self, *args, **kwargs):
¶
NamedUrlWizardView
This renders the form or, if needed, does the http redirects.
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | def get(self, *args, **kwargs):
"""
This renders the form or, if needed, does the http redirects.
"""
step_url = kwargs.get('step', None)
if step_url is None:
if 'reset' in self.request.GET:
self.storage.reset()
self.storage.current_step = self.steps.first
if self.request.GET:
query_string = "?%s" % self.request.GET.urlencode()
else:
query_string = ""
return redirect(self.get_step_url(self.steps.current)
+ query_string)
# is the current step the "done" name/view?
elif step_url == self.done_step_name:
last_step = self.steps.last
return self.render_done(self.get_form(step=last_step,
data=self.storage.get_step_data(last_step),
files=self.storage.get_step_files(last_step)
), **kwargs)
# is the url step name not equal to the step in the storage?
# if yes, change the step in the storage (if name exists)
elif step_url == self.steps.current:
# URL step name and storage step name are equal, render!
return self.render(self.get_form(
data=self.storage.current_step_data,
files=self.storage.current_step_files,
), **kwargs)
elif step_url in self.get_form_list():
self.storage.current_step = step_url
return self.render(self.get_form(
data=self.storage.current_step_data,
files=self.storage.current_step_files,
), **kwargs)
# invalid step name, reset to first and redirect.
else:
self.storage.current_step = self.steps.first
return redirect(self.get_step_url(self.steps.first))
|
WizardView
This method handles GET requests. If a GET request reaches this point, the wizard assumes that the user just starts at the first step or wants to restart the process. The data of the wizard will be resetted before rendering the first step.
243 244 245 246 247 248 249 250 251 252 253 | def get(self, request, *args, **kwargs):
"""
This method handles GET requests.
If a GET request reaches this point, the wizard assumes that the user
just starts at the first step or wants to restart the process.
The data of the wizard will be resetted before rendering the first step.
"""
self.storage.reset()
# reset the current step to the first step.
self.storage.current_step = self.steps.first
return self.render(self.get_form())
|
TemplateView
153 154 155 | def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
|
def
get_all_cleaned_data(self):
WizardView
¶
def
get_all_cleaned_data(self):
WizardView
¶
Returns a merged dictionary of all step cleaned_data dictionaries. If a step contains a `FormSet`, the key will be prefixed with 'formset-' and contain a list of the formset cleaned_data dictionaries.
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | def get_all_cleaned_data(self):
"""
Returns a merged dictionary of all step cleaned_data dictionaries.
If a step contains a `FormSet`, the key will be prefixed with
'formset-' and contain a list of the formset cleaned_data dictionaries.
"""
cleaned_data = {}
for form_key in self.get_form_list():
form_obj = self.get_form(
step=form_key,
data=self.storage.get_step_data(form_key),
files=self.storage.get_step_files(form_key)
)
if form_obj.is_valid():
if isinstance(form_obj.cleaned_data, (tuple, list)):
cleaned_data.update({
'formset-%s' % form_key: form_obj.cleaned_data
})
else:
cleaned_data.update(form_obj.cleaned_data)
return cleaned_data
|
def
get_cleaned_data_for_step(self, step):
WizardView
¶
def
get_cleaned_data_for_step(self, step):
WizardView
¶
Returns the cleaned data for a given `step`. Before returning the cleaned data, the stored values are revalidated through the form. If the data doesn't validate, None will be returned.
480 481 482 483 484 485 486 487 488 489 490 491 492 | def get_cleaned_data_for_step(self, step):
"""
Returns the cleaned data for a given `step`. Before returning the
cleaned data, the stored values are revalidated through the form.
If the data doesn't validate, None will be returned.
"""
if step in self.form_list:
form_obj = self.get_form(step=step,
data=self.storage.get_step_data(step),
files=self.storage.get_step_files(step))
if form_obj.is_valid():
return form_obj.cleaned_data
return None
|
def
get_context_data(self, form, **kwargs):
¶
def
get_context_data(self, form, **kwargs):
¶
NamedUrlWizardView
NamedUrlWizardView provides the url_name of this wizard in the context dict `wizard`.
679 680 681 682 683 684 685 686 | def get_context_data(self, form, **kwargs):
"""
NamedUrlWizardView provides the url_name of this wizard in the context
dict `wizard`.
"""
context = super(NamedUrlWizardView, self).get_context_data(form=form, **kwargs)
context['wizard']['url_name'] = self.url_name
return context
|
WizardView
Returns the template context for a step. You can overwrite this method to add more data for all or some steps. This method returns a dictionary containing the rendered form step. Available template context variables are: * all extra data stored in the storage backend * `wizard` - a dictionary representation of the wizard instance Example: .. code-block:: python class MyWizard(WizardView): def get_context_data(self, form, **kwargs): context = super(MyWizard, self).get_context_data(form=form, **kwargs) if self.steps.current == 'my_step_name': context.update({'another_var': True}) return context
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | def get_context_data(self, form, **kwargs):
"""
Returns the template context for a step. You can overwrite this method
to add more data for all or some steps. This method returns a
dictionary containing the rendered form step. Available template
context variables are:
* all extra data stored in the storage backend
* `wizard` - a dictionary representation of the wizard instance
Example:
.. code-block:: python
class MyWizard(WizardView):
def get_context_data(self, form, **kwargs):
context = super(MyWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'my_step_name':
context.update({'another_var': True})
return context
"""
context = super(WizardView, self).get_context_data(form=form, **kwargs)
context.update(self.storage.extra_data)
context['wizard'] = {
'form': form,
'steps': self.steps,
'management_form': ManagementForm(prefix=self.prefix, initial={
'current_step': self.steps.current,
}),
}
return context
|
ContextMixin
22 23 24 25 | def get_context_data(self, **kwargs):
if 'view' not in kwargs:
kwargs['view'] = self
return kwargs
|
def
get_form(self, step=None, data=None, files=None):
WizardView
¶
def
get_form(self, step=None, data=None, files=None):
WizardView
¶
Constructs the form for a given `step`. If no `step` is defined, the current step will be determined automatically. The form will be initialized using the `data` argument to prefill the new form. If needed, instance or queryset (for `ModelForm` or `ModelFormSet`) will be added too.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | def get_form(self, step=None, data=None, files=None):
"""
Constructs the form for a given `step`. If no `step` is defined, the
current step will be determined automatically.
The form will be initialized using the `data` argument to prefill the
new form. If needed, instance or queryset (for `ModelForm` or
`ModelFormSet`) will be added too.
"""
if step is None:
step = self.steps.current
form_class = self.form_list[step]
# prepare the kwargs for the form instance.
kwargs = self.get_form_kwargs(step)
kwargs.update({
'data': data,
'files': files,
'prefix': self.get_form_prefix(step, form_class),
'initial': self.get_form_initial(step),
})
if issubclass(form_class, (forms.ModelForm, forms.models.BaseInlineFormSet)):
# If the form is based on ModelForm or InlineFormSet,
# add instance if available and not previously set.
kwargs.setdefault('instance', self.get_form_instance(step))
elif issubclass(form_class, forms.models.BaseModelFormSet):
# If the form is based on ModelFormSet, add queryset if available
# and not previous set.
kwargs.setdefault('queryset', self.get_form_instance(step))
return form_class(**kwargs)
|
def
get_form_initial(self, step):
WizardView
¶
def
get_form_initial(self, step):
WizardView
¶
Returns a dictionary which will be passed to the form for `step` as `initial`. If no initial data was provided while initializing the form wizard, an empty dictionary will be returned.
368 369 370 371 372 373 374 | def get_form_initial(self, step):
"""
Returns a dictionary which will be passed to the form for `step`
as `initial`. If no initial data was provided while initializing the
form wizard, an empty dictionary will be returned.
"""
return self.initial_dict.get(step, {})
|
def
get_form_instance(self, step):
WizardView
¶
def
get_form_instance(self, step):
WizardView
¶
Returns an object which will be passed to the form for `step` as `instance`. If no instance object was provided while initializing the form wizard, None will be returned.
376 377 378 379 380 381 382 | def get_form_instance(self, step):
"""
Returns an object which will be passed to the form for `step`
as `instance`. If no instance object was provided while initializing
the form wizard, None will be returned.
"""
return self.instance_dict.get(step, None)
|
def
get_form_kwargs(self, step=None):
WizardView
¶
def
get_form_kwargs(self, step=None):
WizardView
¶
Returns the keyword arguments for instantiating the form (or formset) on the given step.
384 385 386 387 388 389 | def get_form_kwargs(self, step=None):
"""
Returns the keyword arguments for instantiating the form
(or formset) on the given step.
"""
return {}
|
def
get_form_list(self):
WizardView
¶
def
get_form_list(self):
WizardView
¶
This method returns a form_list based on the initial form list but checks if there is a condition method/value in the condition_list. If an entry exists in the condition list, it will call/read the value and respect the result. (True means add the form, False means ignore the form) The form_list is always generated on the fly because condition methods could use data from other (maybe previous forms).
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | def get_form_list(self):
"""
This method returns a form_list based on the initial form list but
checks if there is a condition method/value in the condition_list.
If an entry exists in the condition list, it will call/read the value
and respect the result. (True means add the form, False means ignore
the form)
The form_list is always generated on the fly because condition methods
could use data from other (maybe previous forms).
"""
form_list = OrderedDict()
for form_key, form_class in six.iteritems(self.form_list):
# try to fetch the value from condition list, by default, the form
# gets passed to the new list.
condition = self.condition_dict.get(form_key, True)
if callable(condition):
# call the value if needed, passes the current instance.
condition = condition(self)
if condition:
form_list[form_key] = form_class
return form_list
|
def
get_form_prefix(self, step=None, form=None):
WizardView
¶
def
get_form_prefix(self, step=None, form=None):
WizardView
¶
Returns the prefix which will be used when calling the actual form for the given step. `step` contains the step-name, `form` the form which will be called with the returned prefix. If no step is given, the form_prefix will determine the current step automatically.
355 356 357 358 359 360 361 362 363 364 365 | def get_form_prefix(self, step=None, form=None):
"""
Returns the prefix which will be used when calling the actual form for
the given step. `step` contains the step-name, `form` the form which
will be called with the returned prefix.
If no step is given, the form_prefix will determine the current step
automatically.
"""
if step is None:
step = self.steps.current
return str(step)
|
def
get_form_step_data(self, form):
WizardView
¶
def
get_form_step_data(self, form):
WizardView
¶
Is used to return the raw form data. You may use this method to manipulate the data.
444 445 446 447 448 449 | def get_form_step_data(self, form):
"""
Is used to return the raw form data. You may use this method to
manipulate the data.
"""
return form.data
|
def
get_form_step_files(self, form):
WizardView
¶
def
get_form_step_files(self, form):
WizardView
¶
Is used to return the raw form files. You may use this method to manipulate the data.
451 452 453 454 455 456 | def get_form_step_files(self, form):
"""
Is used to return the raw form files. You may use this method to
manipulate the data.
"""
return form.files
|
def
get_initkwargs(cls, *args, **kwargs):
¶
def
get_initkwargs(cls, *args, **kwargs):
¶
NamedUrlWizardView
We require a url_name to reverse URLs later. Additionally users can pass a done_step_name to change the URL name of the "done" view.
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | @classmethod
def get_initkwargs(cls, *args, **kwargs):
"""
We require a url_name to reverse URLs later. Additionally users can
pass a done_step_name to change the URL name of the "done" view.
"""
assert 'url_name' in kwargs, 'URL name is needed to resolve correct wizard URLs'
extra_kwargs = {
'done_step_name': kwargs.pop('done_step_name', 'done'),
'url_name': kwargs.pop('url_name'),
}
initkwargs = super(NamedUrlWizardView, cls).get_initkwargs(*args, **kwargs)
initkwargs.update(extra_kwargs)
assert initkwargs['done_step_name'] not in initkwargs['form_list'], \
'step name "%s" is reserved for "done" view' % initkwargs['done_step_name']
return initkwargs
|
WizardView
Creates a dict with all needed parameters for the form wizard instances. * `form_list` - is a list of forms. The list entries can be single form classes or tuples of (`step_name`, `form_class`). If you pass a list of forms, the wizardview will convert the class list to (`zero_based_counter`, `form_class`). This is needed to access the form for a specific step. * `initial_dict` - contains a dictionary of initial data dictionaries. The key should be equal to the `step_name` in the `form_list` (or the str of the zero based counter - if no step_names added in the `form_list`) * `instance_dict` - contains a dictionary whose values are model instances if the step is based on a ``ModelForm`` and querysets if the step is based on a ``ModelFormSet``. The key should be equal to the `step_name` in the `form_list`. Same rules as for `initial_dict` apply. * `condition_dict` - contains a dictionary of boolean values or callables. If the value of for a specific `step_name` is callable it will be called with the wizardview instance as the only argument. If the return value is true, the step's form will be used.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | @classmethod
def get_initkwargs(cls, form_list=None, initial_dict=None,
instance_dict=None, condition_dict=None, *args, **kwargs):
"""
Creates a dict with all needed parameters for the form wizard instances.
* `form_list` - is a list of forms. The list entries can be single form
classes or tuples of (`step_name`, `form_class`). If you pass a list
of forms, the wizardview will convert the class list to
(`zero_based_counter`, `form_class`). This is needed to access the
form for a specific step.
* `initial_dict` - contains a dictionary of initial data dictionaries.
The key should be equal to the `step_name` in the `form_list` (or
the str of the zero based counter - if no step_names added in the
`form_list`)
* `instance_dict` - contains a dictionary whose values are model
instances if the step is based on a ``ModelForm`` and querysets if
the step is based on a ``ModelFormSet``. The key should be equal to
the `step_name` in the `form_list`. Same rules as for `initial_dict`
apply.
* `condition_dict` - contains a dictionary of boolean values or
callables. If the value of for a specific `step_name` is callable it
will be called with the wizardview instance as the only argument.
If the return value is true, the step's form will be used.
"""
kwargs.update({
'initial_dict': initial_dict or kwargs.pop('initial_dict',
getattr(cls, 'initial_dict', None)) or {},
'instance_dict': instance_dict or kwargs.pop('instance_dict',
getattr(cls, 'instance_dict', None)) or {},
'condition_dict': condition_dict or kwargs.pop('condition_dict',
getattr(cls, 'condition_dict', None)) or {}
})
form_list = form_list or kwargs.pop('form_list',
getattr(cls, 'form_list', None)) or []
computed_form_list = OrderedDict()
assert len(form_list) > 0, 'at least one form is needed'
# walk through the passed form list
for i, form in enumerate(form_list):
if isinstance(form, (list, tuple)):
# if the element is a tuple, add the tuple to the new created
# sorted dictionary.
computed_form_list[six.text_type(form[0])] = form[1]
else:
# if not, add the form with a zero based counter as unicode
computed_form_list[six.text_type(i)] = form
# walk through the new created list of forms
for form in six.itervalues(computed_form_list):
if issubclass(form, formsets.BaseFormSet):
# if the element is based on BaseFormSet (FormSet/ModelFormSet)
# we need to override the form variable.
form = form.form
# check if any form contains a FileField, if yes, we need a
# file_storage added to the wizardview (by subclassing).
for field in six.itervalues(form.base_fields):
if (isinstance(field, forms.FileField) and
not hasattr(cls, 'file_storage')):
raise NoFileStorageConfigured(
"You need to define 'file_storage' in your "
"wizard view in order to handle file uploads.")
# build the kwargs for the wizardview instances
kwargs['form_list'] = computed_form_list
return kwargs
|
def
get_next_step(self, step=None):
WizardView
¶
def
get_next_step(self, step=None):
WizardView
¶
Returns the next step after the given `step`. If no more steps are available, None will be returned. If the `step` argument is None, the current step will be determined automatically.
494 495 496 497 498 499 500 501 502 503 504 505 506 507 | def get_next_step(self, step=None):
"""
Returns the next step after the given `step`. If no more steps are
available, None will be returned. If the `step` argument is None, the
current step will be determined automatically.
"""
if step is None:
step = self.steps.current
form_list = self.get_form_list()
keys = list(form_list.keys())
key = keys.index(step) + 1
if len(keys) > key:
return keys[key]
return None
|
def
get_prefix(self, *args, **kwargs):
WizardView
¶
def
get_prefix(self, *args, **kwargs):
WizardView
¶
195 196 197 | def get_prefix(self, *args, **kwargs):
# TODO: Add some kind of unique id to prefix
return normalize_name(self.__class__.__name__)
|
def
get_prev_step(self, step=None):
WizardView
¶
def
get_prev_step(self, step=None):
WizardView
¶
Returns the previous step before the given `step`. If there are no steps available, None will be returned. If the `step` argument is None, the current step will be determined automatically.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 | def get_prev_step(self, step=None):
"""
Returns the previous step before the given `step`. If there are no
steps available, None will be returned. If the `step` argument is
None, the current step will be determined automatically.
"""
if step is None:
step = self.steps.current
form_list = self.get_form_list()
keys = list(form_list.keys())
key = keys.index(step) - 1
if key >= 0:
return keys[key]
return None
|
def
get_step_index(self, step=None):
WizardView
¶
def
get_step_index(self, step=None):
WizardView
¶
Returns the index for the given `step` name. If no step is given, the current step will be used to get the index.
524 525 526 527 528 529 530 531 | def get_step_index(self, step=None):
"""
Returns the index for the given `step` name. If no step is given,
the current step will be used to get the index.
"""
if step is None:
step = self.steps.current
return list(self.get_form_list().keys()).index(step)
|
def
get_step_url(self, step):
NamedUrlWizardView
¶
def
get_step_url(self, step):
NamedUrlWizardView
¶
621 622 | def get_step_url(self, step):
return reverse(self.url_name, kwargs={'step': step})
|
def
get_template_names(self):
TemplateResponseMixin
¶
def
get_template_names(self):
TemplateResponseMixin
¶
Returns a list of template names to be used for the request. Must return a list. May not be called if render_to_response is overridden.
135 136 137 138 139 140 141 142 143 144 145 | def get_template_names(self):
"""
Returns a list of template names to be used for the request. Must return
a list. May not be called if render_to_response is overridden.
"""
if self.template_name is None:
raise ImproperlyConfigured(
"TemplateResponseMixin requires either a definition of "
"'template_name' or an implementation of 'get_template_names()'")
else:
return [self.template_name]
|
def
http_method_not_allowed(self, request, *args, **kwargs):
View
¶
def
http_method_not_allowed(self, request, *args, **kwargs):
View
¶
89 90 91 92 93 94 95 96 | def http_method_not_allowed(self, request, *args, **kwargs):
logger.warning('Method Not Allowed (%s): %s', request.method, request.path,
extra={
'status_code': 405,
'request': request
}
)
return http.HttpResponseNotAllowed(self._allowed_methods())
|
def
options(self, request, *args, **kwargs):
View
¶
def
options(self, request, *args, **kwargs):
View
¶
Handles responding to requests for the OPTIONS HTTP verb.
98 99 100 101 102 103 104 105 | def options(self, request, *args, **kwargs):
"""
Handles responding to requests for the OPTIONS HTTP verb.
"""
response = http.HttpResponse()
response['Allow'] = ', '.join(self._allowed_methods())
response['Content-Length'] = '0'
return response
|
def
post(self, *args, **kwargs):
¶
def
post(self, *args, **kwargs):
¶
NamedUrlWizardView
Do a redirect if user presses the prev. step button. The rest of this is super'd from WizardView.
669 670 671 672 673 674 675 676 677 | def post(self, *args, **kwargs):
"""
Do a redirect if user presses the prev. step button. The rest of this
is super'd from WizardView.
"""
wizard_goto_step = self.request.POST.get('wizard_goto_step', None)
if wizard_goto_step and wizard_goto_step in self.get_form_list():
return self.render_goto_step(wizard_goto_step)
return super(NamedUrlWizardView, self).post(*args, **kwargs)
|
WizardView
This method handles POST requests. The wizard will render either the current step (if form validation wasn't successful), the next step (if the current step was stored successful) or the done view (if no more steps are available)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | def post(self, *args, **kwargs):
"""
This method handles POST requests.
The wizard will render either the current step (if form validation
wasn't successful), the next step (if the current step was stored
successful) or the done view (if no more steps are available)
"""
# Look for a wizard_goto_step element in the posted data which
# contains a valid step name. If one was found, render the requested
# form. (This makes stepping back a lot easier).
wizard_goto_step = self.request.POST.get('wizard_goto_step', None)
if wizard_goto_step and wizard_goto_step in self.get_form_list():
return self.render_goto_step(wizard_goto_step)
# Check if form was refreshed
management_form = ManagementForm(self.request.POST, prefix=self.prefix)
if not management_form.is_valid():
raise ValidationError(
_('ManagementForm data is missing or has been tampered.'),
code='missing_management_form',
)
form_current_step = management_form.cleaned_data['current_step']
if (form_current_step != self.steps.current and
self.storage.current_step is not None):
# form refreshed, change current step
self.storage.current_step = form_current_step
# get the form for the current step
form = self.get_form(data=self.request.POST, files=self.request.FILES)
# and try to validate
if form.is_valid():
# if the form is valid, store the cleaned data and files.
self.storage.set_step_data(self.steps.current, self.process_step(form))
self.storage.set_step_files(self.steps.current, self.process_step_files(form))
# check if the current step is the last step
if self.steps.current == self.steps.last:
# no more steps, render done view
return self.render_done(form, **kwargs)
else:
# proceed to the next step
return self.render_next_step(form)
return self.render(form)
|
def
process_step(self, form):
WizardView
¶
def
process_step(self, form):
WizardView
¶
This method is used to postprocess the form data. By default, it returns the raw `form.data` dictionary.
421 422 423 424 425 426 | def process_step(self, form):
"""
This method is used to postprocess the form data. By default, it
returns the raw `form.data` dictionary.
"""
return self.get_form_step_data(form)
|
def
process_step_files(self, form):
WizardView
¶
def
process_step_files(self, form):
WizardView
¶
This method is used to postprocess the form files. By default, it returns the raw `form.files` dictionary.
428 429 430 431 432 433 | def process_step_files(self, form):
"""
This method is used to postprocess the form files. By default, it
returns the raw `form.files` dictionary.
"""
return self.get_form_step_files(form)
|
def
render(self, form=None, **kwargs):
WizardView
¶
def
render(self, form=None, **kwargs):
WizardView
¶
Returns a ``HttpResponse`` containing all needed context data.
565 566 567 568 569 570 571 | def render(self, form=None, **kwargs):
"""
Returns a ``HttpResponse`` containing all needed context data.
"""
form = form or self.get_form()
context = self.get_context_data(form=form, **kwargs)
return self.render_to_response(context)
|
def
render_done(self, form, **kwargs):
¶
def
render_done(self, form, **kwargs):
¶
NamedUrlWizardView
When rendering the done view, we have to redirect first (if the URL name doesn't fit).
713 714 715 716 717 718 719 720 | def render_done(self, form, **kwargs):
"""
When rendering the done view, we have to redirect first (if the URL
name doesn't fit).
"""
if kwargs.get('step', None) != self.done_step_name:
return redirect(self.get_step_url(self.done_step_name))
return super(NamedUrlWizardView, self).render_done(form, **kwargs)
|
WizardView
This method gets called when all forms passed. The method should also re-validate all steps to prevent manipulation. If any form fails to validate, `render_revalidation_failure` should get called. If everything is fine call `done`.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | def render_done(self, form, **kwargs):
"""
This method gets called when all forms passed. The method should also
re-validate all steps to prevent manipulation. If any form fails to
validate, `render_revalidation_failure` should get called.
If everything is fine call `done`.
"""
final_forms = OrderedDict()
# walk through the form list and try to validate the data again.
for form_key in self.get_form_list():
form_obj = self.get_form(step=form_key,
data=self.storage.get_step_data(form_key),
files=self.storage.get_step_files(form_key))
if not form_obj.is_valid():
return self.render_revalidation_failure(form_key, form_obj, **kwargs)
final_forms[form_key] = form_obj
# render the done view and reset the wizard before returning the
# response. This is needed to prevent from rendering done with the
# same data twice.
done_response = self.done(final_forms.values(), form_dict=final_forms, **kwargs)
self.storage.reset()
return done_response
|
def
render_goto_step(self, goto_step, **kwargs):
¶
def
render_goto_step(self, goto_step, **kwargs):
¶
NamedUrlWizardView
This method gets called when the current step has to be changed. `goto_step` contains the requested step to go to.
697 698 699 700 701 702 703 | def render_goto_step(self, goto_step, **kwargs):
"""
This method gets called when the current step has to be changed.
`goto_step` contains the requested step to go to.
"""
self.storage.current_step = goto_step
return redirect(self.get_step_url(goto_step))
|
WizardView
This method gets called when the current step has to be changed. `goto_step` contains the requested step to go to.
320 321 322 323 324 325 326 327 328 329 | def render_goto_step(self, goto_step, **kwargs):
"""
This method gets called when the current step has to be changed.
`goto_step` contains the requested step to go to.
"""
self.storage.current_step = goto_step
form = self.get_form(
data=self.storage.get_step_data(self.steps.current),
files=self.storage.get_step_files(self.steps.current))
return self.render(form)
|
def
render_next_step(self, form, **kwargs):
¶
def
render_next_step(self, form, **kwargs):
¶
NamedUrlWizardView
When using the NamedUrlWizardView, we have to redirect to update the browser's URL to match the shown step.
688 689 690 691 692 693 694 695 | def render_next_step(self, form, **kwargs):
"""
When using the NamedUrlWizardView, we have to redirect to update the
browser's URL to match the shown step.
"""
next_step = self.get_next_step()
self.storage.current_step = next_step
return redirect(self.get_step_url(next_step))
|
WizardView
This method gets called when the next step/form should be rendered. `form` contains the last/current form.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 | def render_next_step(self, form, **kwargs):
"""
This method gets called when the next step/form should be rendered.
`form` contains the last/current form.
"""
# get the form instance based on the data from the storage backend
# (if available).
next_step = self.steps.next
new_form = self.get_form(next_step,
data=self.storage.get_step_data(next_step),
files=self.storage.get_step_files(next_step))
# change the stored current step
self.storage.current_step = next_step
return self.render(new_form, **kwargs)
|
def
render_revalidation_failure(self, failed_step, form, **kwargs):
¶
def
render_revalidation_failure(self, failed_step, form, **kwargs):
¶
NamedUrlWizardView
When a step fails, we have to redirect the user to the first failing step.
705 706 707 708 709 710 711 | def render_revalidation_failure(self, failed_step, form, **kwargs):
"""
When a step fails, we have to redirect the user to the first failing
step.
"""
self.storage.current_step = failed_step
return redirect(self.get_step_url(failed_step))
|
WizardView
Gets called when a form doesn't validate when rendering the done view. By default, it changes the current step to failing forms step and renders the form.
435 436 437 438 439 440 441 442 | def render_revalidation_failure(self, step, form, **kwargs):
"""
Gets called when a form doesn't validate when rendering the done
view. By default, it changes the current step to failing forms step
and renders the form.
"""
self.storage.current_step = step
return self.render(form, **kwargs)
|
def
render_to_response(self, context, **response_kwargs):
TemplateResponseMixin
¶
def
render_to_response(self, context, **response_kwargs):
TemplateResponseMixin
¶
Returns a response, using the `response_class` for this view, with a template rendered with the given context. If any keyword arguments are provided, they will be passed to the constructor of the response class.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 | def render_to_response(self, context, **response_kwargs):
"""
Returns a response, using the `response_class` for this
view, with a template rendered with the given context.
If any keyword arguments are provided, they will be
passed to the constructor of the response class.
"""
response_kwargs.setdefault('content_type', self.content_type)
return self.response_class(
request=self.request,
template=self.get_template_names(),
context=context,
**response_kwargs
)
|