Fixed #18872 -- Added prefix to FormMixin
Thanks @ibustama for the initial patch and dragonsnaker for opening the report.
This commit is contained in:
parent
ef79582e86
commit
ef37b23050
1
AUTHORS
1
AUTHORS
|
@ -249,6 +249,7 @@ answer newbie questions, and generally made Django that much better:
|
||||||
martin.glueck@gmail.com
|
martin.glueck@gmail.com
|
||||||
Ben Godfrey <http://aftnn.org>
|
Ben Godfrey <http://aftnn.org>
|
||||||
GomoX <gomo@datafull.com>
|
GomoX <gomo@datafull.com>
|
||||||
|
Gil Gonçalves <lursty@gmail.com>
|
||||||
Guilherme Mesquita Gondim <semente@taurinus.org>
|
Guilherme Mesquita Gondim <semente@taurinus.org>
|
||||||
Mario Gonzalez <gonzalemario@gmail.com>
|
Mario Gonzalez <gonzalemario@gmail.com>
|
||||||
David Gouldin <dgouldin@gmail.com>
|
David Gouldin <dgouldin@gmail.com>
|
||||||
|
|
|
@ -17,6 +17,7 @@ class FormMixin(ContextMixin):
|
||||||
initial = {}
|
initial = {}
|
||||||
form_class = None
|
form_class = None
|
||||||
success_url = None
|
success_url = None
|
||||||
|
prefix = None
|
||||||
|
|
||||||
def get_initial(self):
|
def get_initial(self):
|
||||||
"""
|
"""
|
||||||
|
@ -24,6 +25,12 @@ class FormMixin(ContextMixin):
|
||||||
"""
|
"""
|
||||||
return self.initial.copy()
|
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):
|
def get_form_class(self):
|
||||||
"""
|
"""
|
||||||
Returns the form class to use in this view
|
Returns the form class to use in this view
|
||||||
|
@ -40,7 +47,11 @@ class FormMixin(ContextMixin):
|
||||||
"""
|
"""
|
||||||
Returns the keyword arguments for instantiating the form.
|
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'):
|
if self.request.method in ('POST', 'PUT'):
|
||||||
kwargs.update({
|
kwargs.update({
|
||||||
'data': self.request.POST,
|
'data': self.request.POST,
|
||||||
|
|
|
@ -35,6 +35,10 @@ FormMixin
|
||||||
|
|
||||||
The URL to redirect to when the form is successfully processed.
|
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()
|
.. method:: get_initial()
|
||||||
|
|
||||||
Retrieve initial data for the form. By default, returns a copy of
|
Retrieve initial data for the form. By default, returns a copy of
|
||||||
|
|
|
@ -731,6 +731,9 @@ Miscellaneous
|
||||||
of the admin views. You should update your custom templates if they use the
|
of the admin views. You should update your custom templates if they use the
|
||||||
previous parameter name.
|
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
|
Features deprecated in 1.6
|
||||||
==========================
|
==========================
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,9 @@ from django.core.urlresolvers import reverse
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.utils.unittest import expectedFailure
|
from django.utils.unittest import expectedFailure
|
||||||
|
from django.test.client import RequestFactory
|
||||||
from django.views.generic.base import View
|
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 . import views
|
||||||
from .models import Artist, Author
|
from .models import Artist, Author
|
||||||
|
@ -22,6 +23,24 @@ class FormMixinTests(TestCase):
|
||||||
initial_2 = FormMixin().get_initial()
|
initial_2 = FormMixin().get_initial()
|
||||||
self.assertNotEqual(initial_1, initial_2)
|
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):
|
class BasicFormTests(TestCase):
|
||||||
urls = 'generic_views.urls'
|
urls = 'generic_views.urls'
|
||||||
|
|
Loading…
Reference in New Issue