Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Marc Tamlyn authored June 22, 2013
1  AUTHORS
@@ -249,6 +249,7 @@ answer newbie questions, and generally made Django that much better:
249 249
     martin.glueck@gmail.com
250 250
     Ben Godfrey <http://aftnn.org>
251 251
     GomoX <gomo@datafull.com>
  252
+    Gil Gonçalves <lursty@gmail.com>
252 253
     Guilherme Mesquita Gondim <semente@taurinus.org>
253 254
     Mario Gonzalez <gonzalemario@gmail.com>
254 255
     David Gouldin <dgouldin@gmail.com>
13  django/views/generic/edit.py
@@ -17,6 +17,7 @@ class FormMixin(ContextMixin):
17 17
     initial = {}
18 18
     form_class = None
19 19
     success_url = None
  20
+    prefix = None
20 21
 
21 22
     def get_initial(self):
22 23
         """
@@ -24,6 +25,12 @@ def get_initial(self):
24 25
         """
25 26
         return self.initial.copy()
26 27
 
  28
+    def get_prefix(self):
  29
+        """
  30
+        Returns the prefix to use for forms on this view
  31
+        """
  32
+        return self.prefix
  33
+
27 34
     def get_form_class(self):
28 35
         """
29 36
         Returns the form class to use in this view
@@ -40,7 +47,11 @@ def get_form_kwargs(self):
40 47
         """
41 48
         Returns the keyword arguments for instantiating the form.
42 49
         """
43  
-        kwargs = {'initial': self.get_initial()}
  50
+        kwargs = {
  51
+            'initial': self.get_initial(),
  52
+            'prefix': self.get_prefix(),
  53
+        }
  54
+
44 55
         if self.request.method in ('POST', 'PUT'):
45 56
             kwargs.update({
46 57
                 'data': self.request.POST,
4  docs/ref/class-based-views/mixins-editing.txt
@@ -35,6 +35,10 @@ FormMixin
35 35
 
36 36
         The URL to redirect to when the form is successfully processed.
37 37
 
  38
+    .. attribute:: prefix
  39
+
  40
+        Sets the :attr:`~django.forms.Form.prefix` for the generated form.
  41
+
38 42
     .. method:: get_initial()
39 43
 
40 44
         Retrieve initial data for the form. By default, returns a copy of
3  docs/releases/1.6.txt
@@ -731,6 +731,9 @@ Miscellaneous
731 731
   of the admin views. You should update your custom templates if they use the
732 732
   previous parameter name.
733 733
 
  734
+* Added :attr:`~django.views.generic.edit.FormMixin.prefix` to allow you to
  735
+  customize the prefix on the form.
  736
+
734 737
 Features deprecated in 1.6
735 738
 ==========================
736 739
 
21  tests/generic_views/test_edit.py
@@ -7,8 +7,9 @@
7 7
 from django import forms
8 8
 from django.test import TestCase
9 9
 from django.utils.unittest import expectedFailure
  10
+from django.test.client import RequestFactory
10 11
 from django.views.generic.base import View
11  
-from django.views.generic.edit import FormMixin, CreateView, UpdateView
  12
+from django.views.generic.edit import FormMixin, CreateView
12 13
 
13 14
 from . import views
14 15
 from .models import Artist, Author
@@ -22,6 +23,24 @@ def test_initial_data(self):
22 23
         initial_2 = FormMixin().get_initial()
23 24
         self.assertNotEqual(initial_1, initial_2)
24 25
 
  26
+    def test_get_prefix(self):
  27
+        """ Test prefix can be set (see #18872) """
  28
+        test_string = 'test'
  29
+
  30
+        rf = RequestFactory()
  31
+        get_request = rf.get('/')
  32
+
  33
+        class TestFormMixin(FormMixin):
  34
+            request = get_request
  35
+
  36
+        default_kwargs = TestFormMixin().get_form_kwargs()
  37
+        self.assertEqual(None, default_kwargs.get('prefix'))
  38
+
  39
+        set_mixin = TestFormMixin()
  40
+        set_mixin.prefix = test_string
  41
+        set_kwargs = set_mixin.get_form_kwargs()
  42
+        self.assertEqual(test_string, set_kwargs.get('prefix'))
  43
+
25 44
 
26 45
 class BasicFormTests(TestCase):
27 46
     urls = 'generic_views.urls'

0 notes on commit 257a137

Please sign in to comment.
Something went wrong with that request. Please try again.