diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index e51cdf5a3d5..97a6c0a698f 100644 --- a/django/views/generic/edit.py +++ b/django/views/generic/edit.py @@ -1,6 +1,7 @@ from django.forms import models as model_forms from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponseRedirect +from django.utils.encoding import force_text from django.views.generic.base import TemplateResponseMixin, ContextMixin, View from django.views.generic.detail import (SingleObjectMixin, SingleObjectTemplateResponseMixin, BaseDetailView) @@ -50,7 +51,8 @@ class FormMixin(ContextMixin): Returns the supplied success URL. """ if self.success_url: - url = self.success_url + # Forcing possible reverse_lazy evaluation + url = force_text(self.success_url) else: raise ImproperlyConfigured( "No URL to redirect to. Provide a success_url.") diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py index 16f4da8efe0..0f1eb3cca72 100644 --- a/tests/regressiontests/generic_views/edit.py +++ b/tests/regressiontests/generic_views/edit.py @@ -20,6 +20,15 @@ class FormMixinTests(TestCase): initial_2 = FormMixin().get_initial() self.assertNotEqual(initial_1, initial_2) + +class BasicFormTests(TestCase): + urls = 'regressiontests.generic_views.urls' + + def test_post_data(self): + res = self.client.post('/contact/', {'name': "Me", 'message': "Hello"}) + self.assertRedirects(res, 'http://testserver/list/authors/') + + class ModelFormMixinTests(TestCase): def test_get_form(self): form_class = views.AuthorGetQuerySetFormView().get_form_class() diff --git a/tests/regressiontests/generic_views/forms.py b/tests/regressiontests/generic_views/forms.py index a78242f5558..e036ad8afca 100644 --- a/tests/regressiontests/generic_views/forms.py +++ b/tests/regressiontests/generic_views/forms.py @@ -11,3 +11,8 @@ class AuthorForm(forms.ModelForm): class Meta: model = Author + + +class ContactForm(forms.Form): + name = forms.CharField() + message = forms.CharField(widget=forms.Textarea) diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py index c985ad33098..3f80600efe6 100644 --- a/tests/regressiontests/generic_views/tests.py +++ b/tests/regressiontests/generic_views/tests.py @@ -6,6 +6,6 @@ from .dates import (ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests) from .detail import DetailViewTest -from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests, - UpdateViewTests, DeleteViewTests) +from .edit import (FormMixinTests, BasicFormTests, ModelFormMixinTests, + CreateViewTests, UpdateViewTests, DeleteViewTests) from .list import ListViewTests diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py index 4daf53dbafc..34b37fa7c43 100644 --- a/tests/regressiontests/generic_views/urls.py +++ b/tests/regressiontests/generic_views/urls.py @@ -56,6 +56,10 @@ urlpatterns = patterns('', (r'^detail/nonmodel/1/$', views.NonModelDetail.as_view()), + # FormView + (r'^contact/$', + views.ContactView.as_view()), + # Create/UpdateView (r'^edit/artists/create/$', views.ArtistCreate.as_view()), diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py index f7fcf6f5099..71e78e82c77 100644 --- a/tests/regressiontests/generic_views/views.py +++ b/tests/regressiontests/generic_views/views.py @@ -2,11 +2,11 @@ from __future__ import absolute_import from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, reverse_lazy from django.utils.decorators import method_decorator from django.views import generic -from .forms import AuthorForm +from .forms import AuthorForm, ContactForm from .models import Artist, Author, Book, Page, BookSigning @@ -75,6 +75,13 @@ class AuthorListCustomPaginator(AuthorList): orphans=2, allow_empty_first_page=allow_empty_first_page) + +class ContactView(generic.FormView): + form_class = ContactForm + success_url = reverse_lazy('authors_list') + template_name = 'generic_views/form.html' + + class ArtistCreate(generic.CreateView): model = Artist