0
0
mirror of https://github.com/django/django.git synced 2024-11-30 07:06:18 +01:00

Merge pull request #1294 from LuRsT/added_prefix_to_form_mixin

Fixed #18872 -- Added prefix to FormMixin
This commit is contained in:
Marc Tamlyn 2013-06-22 04:19:24 -07:00
commit 257a137c43
5 changed files with 40 additions and 2 deletions

View File

@ -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 File

@ -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,

View File

@ -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 File

@ -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 File

@ -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'