mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
Fixed #8001 -- Made redirections after add/edit in admin customizable.
Also fixes #18310.
This commit is contained in:
parent
db598dd8a0
commit
0b908b92a2
4 changed files with 190 additions and 48 deletions
|
@ -50,3 +50,40 @@ class ActionAdmin(admin.ModelAdmin):
|
|||
|
||||
|
||||
admin.site.register(Action, ActionAdmin)
|
||||
|
||||
|
||||
class Person(models.Model):
|
||||
nick = models.CharField(max_length=20)
|
||||
|
||||
|
||||
class PersonAdmin(admin.ModelAdmin):
|
||||
"""A custom ModelAdmin that customizes the deprecated post_url_continue
|
||||
argument to response_add()"""
|
||||
def response_add(self, request, obj, post_url_continue='../%s/continue/',
|
||||
continue_url=None, add_url=None, hasperm_url=None,
|
||||
noperm_url=None):
|
||||
return super(PersonAdmin, self).response_add(request, obj,
|
||||
post_url_continue,
|
||||
continue_url, add_url,
|
||||
hasperm_url, noperm_url)
|
||||
|
||||
|
||||
admin.site.register(Person, PersonAdmin)
|
||||
|
||||
|
||||
class City(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
|
||||
|
||||
class CityAdmin(admin.ModelAdmin):
|
||||
"""A custom ModelAdmin that redirects to the changelist when the user
|
||||
presses the 'Save and add another' button when adding a model instance."""
|
||||
def response_add(self, request, obj,
|
||||
add_another_url='admin:admin_custom_urls_city_changelist',
|
||||
**kwargs):
|
||||
return super(CityAdmin, self).response_add(request, obj,
|
||||
add_another_url=add_another_url,
|
||||
**kwargs)
|
||||
|
||||
|
||||
admin.site.register(City, CityAdmin)
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
import warnings
|
||||
|
||||
from django.contrib.admin.util import quote
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template.response import TemplateResponse
|
||||
from django.test import TestCase
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from .models import Action
|
||||
from .models import Action, Person, City
|
||||
|
||||
|
||||
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
||||
|
@ -81,3 +83,45 @@ class AdminCustomUrlsTest(TestCase):
|
|||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'Change action')
|
||||
self.assertContains(response, 'value="path/to/html/document.html"')
|
||||
|
||||
|
||||
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
|
||||
class CustomUrlsWorkflowTests(TestCase):
|
||||
fixtures = ['users.json']
|
||||
|
||||
def setUp(self):
|
||||
self.client.login(username='super', password='secret')
|
||||
|
||||
def tearDown(self):
|
||||
self.client.logout()
|
||||
|
||||
def test_old_argument_deprecation(self):
|
||||
"""Test reporting of post_url_continue deprecation."""
|
||||
post_data = {
|
||||
'nick': 'johndoe',
|
||||
}
|
||||
cnt = Person.objects.count()
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
response = self.client.post(reverse('admin:admin_custom_urls_person_add'), post_data)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(Person.objects.count(), cnt + 1)
|
||||
# We should get a DeprecationWarning
|
||||
self.assertEqual(len(w), 1)
|
||||
self.assertTrue(isinstance(w[0].message, DeprecationWarning))
|
||||
|
||||
def test_custom_add_another_redirect(self):
|
||||
"""Test customizability of post-object-creation redirect URL."""
|
||||
post_data = {
|
||||
'name': 'Rome',
|
||||
'_addanother': '1',
|
||||
}
|
||||
cnt = City.objects.count()
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
# POST to the view whose post-object-creation redir URL argument we
|
||||
# are customizing (object creation)
|
||||
response = self.client.post(reverse('admin:admin_custom_urls_city_add'), post_data)
|
||||
self.assertEqual(City.objects.count(), cnt + 1)
|
||||
# Check that it redirected to the URL we set
|
||||
self.assertRedirects(response, reverse('admin:admin_custom_urls_city_changelist'))
|
||||
self.assertEqual(len(w), 0) # We should get no DeprecationWarning
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue