From ef37b23050637da643b47b1ee744702d4d603f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gilberto=20Gon=C3=A7alves?= Date: Sat, 22 Jun 2013 12:12:43 +0100 Subject: [PATCH] Fixed #18872 -- Added prefix to FormMixin Thanks @ibustama for the initial patch and dragonsnaker for opening the report. --- AUTHORS | 1 + django/views/generic/edit.py | 13 +++++++++++- docs/ref/class-based-views/mixins-editing.txt | 4 ++++ docs/releases/1.6.txt | 3 +++ tests/generic_views/test_edit.py | 21 ++++++++++++++++++- 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3eb0a68be9..e4803dbe9a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -249,6 +249,7 @@ answer newbie questions, and generally made Django that much better: martin.glueck@gmail.com Ben Godfrey GomoX + Gil Gonçalves Guilherme Mesquita Gondim Mario Gonzalez David Gouldin diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index b31d7a218f..193071efc5 100644 --- a/django/views/generic/edit.py +++ b/django/views/generic/edit.py @@ -17,6 +17,7 @@ class FormMixin(ContextMixin): initial = {} form_class = None success_url = None + prefix = None def get_initial(self): """ @@ -24,6 +25,12 @@ class FormMixin(ContextMixin): """ return self.initial.copy() + def get_prefix(self): + """ + Returns the prefix to use for forms on this view + """ + return self.prefix + def get_form_class(self): """ Returns the form class to use in this view @@ -40,7 +47,11 @@ class FormMixin(ContextMixin): """ Returns the keyword arguments for instantiating the form. """ - kwargs = {'initial': self.get_initial()} + kwargs = { + 'initial': self.get_initial(), + 'prefix': self.get_prefix(), + } + if self.request.method in ('POST', 'PUT'): kwargs.update({ 'data': self.request.POST, diff --git a/docs/ref/class-based-views/mixins-editing.txt b/docs/ref/class-based-views/mixins-editing.txt index 48d363b3b2..a0160610d2 100644 --- a/docs/ref/class-based-views/mixins-editing.txt +++ b/docs/ref/class-based-views/mixins-editing.txt @@ -35,6 +35,10 @@ FormMixin The URL to redirect to when the form is successfully processed. + .. attribute:: prefix + + Sets the :attr:`~django.forms.Form.prefix` for the generated form. + .. method:: get_initial() Retrieve initial data for the form. By default, returns a copy of diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index 16e0b94a9d..95bfedc74e 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -731,6 +731,9 @@ Miscellaneous of the admin views. You should update your custom templates if they use the previous parameter name. +* Added :attr:`~django.views.generic.edit.FormMixin.prefix` to allow you to + customize the prefix on the form. + Features deprecated in 1.6 ========================== diff --git a/tests/generic_views/test_edit.py b/tests/generic_views/test_edit.py index 435e48ba99..84d18ebcb2 100644 --- a/tests/generic_views/test_edit.py +++ b/tests/generic_views/test_edit.py @@ -7,8 +7,9 @@ from django.core.urlresolvers import reverse from django import forms from django.test import TestCase from django.utils.unittest import expectedFailure +from django.test.client import RequestFactory from django.views.generic.base import View -from django.views.generic.edit import FormMixin, CreateView, UpdateView +from django.views.generic.edit import FormMixin, CreateView from . import views from .models import Artist, Author @@ -22,6 +23,24 @@ class FormMixinTests(TestCase): initial_2 = FormMixin().get_initial() self.assertNotEqual(initial_1, initial_2) + def test_get_prefix(self): + """ Test prefix can be set (see #18872) """ + test_string = 'test' + + rf = RequestFactory() + get_request = rf.get('/') + + class TestFormMixin(FormMixin): + request = get_request + + default_kwargs = TestFormMixin().get_form_kwargs() + self.assertEqual(None, default_kwargs.get('prefix')) + + set_mixin = TestFormMixin() + set_mixin.prefix = test_string + set_kwargs = set_mixin.get_form_kwargs() + self.assertEqual(test_string, set_kwargs.get('prefix')) + class BasicFormTests(TestCase): urls = 'generic_views.urls'