Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1294 from LuRsT/added_prefix_to_form_mixin

Fixed #18872 -- Added prefix to FormMixin
  • Loading branch information...
commit 257a137c430cd325f3deeda8daffbf03a3cb20fd 2 parents ef79582 + ef37b23
@mjtamlyn mjtamlyn authored
View
1  AUTHORS
@@ -249,6 +249,7 @@ answer newbie questions, and generally made Django that much better:
martin.glueck@gmail.com
Ben Godfrey <http://aftnn.org>
GomoX <gomo@datafull.com>
+ Gil Gonçalves <lursty@gmail.com>
Guilherme Mesquita Gondim <semente@taurinus.org>
Mario Gonzalez <gonzalemario@gmail.com>
David Gouldin <dgouldin@gmail.com>
View
13 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 @@ def get_initial(self):
"""
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 @@ def get_form_kwargs(self):
"""
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,
View
4 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
View
3  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
==========================
View
21 tests/generic_views/test_edit.py
@@ -7,8 +7,9 @@
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 @@ def test_initial_data(self):
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'
Please sign in to comment.
Something went wrong with that request. Please try again.