Fixed #19378 -- Ensured get_success_url returns a non-lazy URL

This commit is contained in:
Claude Paroz 2012-12-04 13:18:57 +01:00
parent 501c7a221c
commit 795ac7deda
6 changed files with 32 additions and 5 deletions

View File

@ -1,6 +1,7 @@
from django.forms import models as model_forms from django.forms import models as model_forms
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseRedirect 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.base import TemplateResponseMixin, ContextMixin, View
from django.views.generic.detail import (SingleObjectMixin, from django.views.generic.detail import (SingleObjectMixin,
SingleObjectTemplateResponseMixin, BaseDetailView) SingleObjectTemplateResponseMixin, BaseDetailView)
@ -50,7 +51,8 @@ class FormMixin(ContextMixin):
Returns the supplied success URL. Returns the supplied success URL.
""" """
if self.success_url: if self.success_url:
url = self.success_url # Forcing possible reverse_lazy evaluation
url = force_text(self.success_url)
else: else:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"No URL to redirect to. Provide a success_url.") "No URL to redirect to. Provide a success_url.")

View File

@ -20,6 +20,15 @@ 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)
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): class ModelFormMixinTests(TestCase):
def test_get_form(self): def test_get_form(self):
form_class = views.AuthorGetQuerySetFormView().get_form_class() form_class = views.AuthorGetQuerySetFormView().get_form_class()

View File

@ -11,3 +11,8 @@ class AuthorForm(forms.ModelForm):
class Meta: class Meta:
model = Author model = Author
class ContactForm(forms.Form):
name = forms.CharField()
message = forms.CharField(widget=forms.Textarea)

View File

@ -6,6 +6,6 @@ from .dates import (ArchiveIndexViewTests, YearArchiveViewTests,
MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
DateDetailViewTests) DateDetailViewTests)
from .detail import DetailViewTest from .detail import DetailViewTest
from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests, from .edit import (FormMixinTests, BasicFormTests, ModelFormMixinTests,
UpdateViewTests, DeleteViewTests) CreateViewTests, UpdateViewTests, DeleteViewTests)
from .list import ListViewTests from .list import ListViewTests

View File

@ -56,6 +56,10 @@ urlpatterns = patterns('',
(r'^detail/nonmodel/1/$', (r'^detail/nonmodel/1/$',
views.NonModelDetail.as_view()), views.NonModelDetail.as_view()),
# FormView
(r'^contact/$',
views.ContactView.as_view()),
# Create/UpdateView # Create/UpdateView
(r'^edit/artists/create/$', (r'^edit/artists/create/$',
views.ArtistCreate.as_view()), views.ArtistCreate.as_view()),

View File

@ -2,11 +2,11 @@ from __future__ import absolute_import
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator 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.utils.decorators import method_decorator
from django.views import generic from django.views import generic
from .forms import AuthorForm from .forms import AuthorForm, ContactForm
from .models import Artist, Author, Book, Page, BookSigning from .models import Artist, Author, Book, Page, BookSigning
@ -75,6 +75,13 @@ class AuthorListCustomPaginator(AuthorList):
orphans=2, orphans=2,
allow_empty_first_page=allow_empty_first_page) 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): class ArtistCreate(generic.CreateView):
model = Artist model = Artist