Ticket #19378: 19378-1.diff

File 19378-1.diff, 4.5 KB (added by claudep, 2 years ago)

Force reverse_lazy evaluation

  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index e51cdf5..97a6c0a 100644
    a b  
    11from django.forms import models as model_forms
    22from django.core.exceptions import ImproperlyConfigured
    33from django.http import HttpResponseRedirect
     4from django.utils.encoding import force_text
    45from django.views.generic.base import TemplateResponseMixin, ContextMixin, View
    56from django.views.generic.detail import (SingleObjectMixin,
    67                        SingleObjectTemplateResponseMixin, BaseDetailView)
    class FormMixin(ContextMixin): 
    5051        Returns the supplied success URL.
    5152        """
    5253        if self.success_url:
    53             url = self.success_url
     54            # Forcing possible reverse_lazy evaluation
     55            url = force_text(self.success_url)
    5456        else:
    5557            raise ImproperlyConfigured(
    5658                "No URL to redirect to. Provide a success_url.")
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 16f4da8..0f1eb3c 100644
    a b class FormMixinTests(TestCase): 
    2020         initial_2 = FormMixin().get_initial()
    2121         self.assertNotEqual(initial_1, initial_2)
    2222
     23
     24class BasicFormTests(TestCase):
     25    urls = 'regressiontests.generic_views.urls'
     26
     27    def test_post_data(self):
     28        res = self.client.post('/contact/', {'name': "Me", 'message': "Hello"})
     29        self.assertRedirects(res, 'http://testserver/list/authors/')
     30
     31
    2332class ModelFormMixinTests(TestCase):
    2433    def test_get_form(self):
    2534        form_class = views.AuthorGetQuerySetFormView().get_form_class()
  • tests/regressiontests/generic_views/forms.py

    diff --git a/tests/regressiontests/generic_views/forms.py b/tests/regressiontests/generic_views/forms.py
    index a78242f..e036ad8 100644
    a b class AuthorForm(forms.ModelForm): 
    1111
    1212    class Meta:
    1313        model = Author
     14
     15
     16class ContactForm(forms.Form):
     17    name = forms.CharField()
     18    message = forms.CharField(widget=forms.Textarea)
  • tests/regressiontests/generic_views/tests.py

    diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py
    index c985ad3..3f80600 100644
    a b from .dates import (ArchiveIndexViewTests, YearArchiveViewTests, 
    66    MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
    77    DateDetailViewTests)
    88from .detail import DetailViewTest
    9 from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests,
    10     UpdateViewTests, DeleteViewTests)
     9from .edit import (FormMixinTests, BasicFormTests, ModelFormMixinTests,
     10    CreateViewTests, UpdateViewTests, DeleteViewTests)
    1111from .list import ListViewTests
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index a212b83..9caf193 100644
    a b urlpatterns = patterns('', 
    5656    (r'^detail/nonmodel/1/$',
    5757        views.NonModelDetail.as_view()),
    5858
     59    # FormView
     60    (r'^contact/$',
     61        views.ContactView.as_view()),
     62
    5963    # Create/UpdateView
    6064    (r'^edit/artists/create/$',
    6165        views.ArtistCreate.as_view()),
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index f7fcf6f..71e78e8 100644
    a b from __future__ import absolute_import 
    22
    33from django.contrib.auth.decorators import login_required
    44from django.core.paginator import Paginator
    5 from django.core.urlresolvers import reverse
     5from django.core.urlresolvers import reverse, reverse_lazy
    66from django.utils.decorators import method_decorator
    77from django.views import generic
    88
    9 from .forms import AuthorForm
     9from .forms import AuthorForm, ContactForm
    1010from .models import Artist, Author, Book, Page, BookSigning
    1111
    1212
    class AuthorListCustomPaginator(AuthorList): 
    7575            orphans=2,
    7676            allow_empty_first_page=allow_empty_first_page)
    7777
     78
     79class ContactView(generic.FormView):
     80    form_class = ContactForm
     81    success_url = reverse_lazy('authors_list')
     82    template_name = 'generic_views/form.html'
     83
     84
    7885class ArtistCreate(generic.CreateView):
    7986    model = Artist
    8087
Back to Top