| | 204 | def test_update_get_object(self): |
| | 205 | a = Author.objects.create( |
| | 206 | name='Randall Munroe', |
| | 207 | slug='randall-munroe', |
| | 208 | ) |
| | 209 | res = self.client.get('/edit/author/update/') |
| | 210 | self.assertEqual(res.status_code, 200) |
| | 211 | self.assertTrue(isinstance(res.context['form'], forms.ModelForm)) |
| | 212 | self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk)) |
| | 213 | self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk)) |
| | 214 | self.assertTemplateUsed(res, 'generic_views/author_form.html') |
| | 215 | |
| | 216 | # Modification with both POST and PUT (browser compatible) |
| | 217 | res = self.client.post('/edit/author/update/' % a.pk, |
| | 218 | {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'}) |
| | 219 | self.assertEqual(res.status_code, 302) |
| | 220 | self.assertRedirects(res, 'http://testserver/list/authors/') |
| | 221 | self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>']) |
| | 222 | |
| | 223 | def test_update_get_object_with_redirect(self): |
| | 224 | a = Author.objects.create( |
| | 225 | name='Randall Munroe', |
| | 226 | slug='randall-munroe', |
| | 227 | ) |
| | 228 | res = self.client.get('/edit/author/update/redirect') |
| | 229 | self.assertEqual(res.status_code, 200) |
| | 230 | self.assertTrue(isinstance(res.context['form'], forms.ModelForm)) |
| | 231 | self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk)) |
| | 232 | self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk)) |
| | 233 | self.assertTemplateUsed(res, 'generic_views/author_form.html') |
| | 234 | |
| | 235 | # Modification with both POST and PUT (browser compatible) |
| | 236 | res = self.client.post('/edit/author/update/redirect' % a.pk, |
| | 237 | {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'}) |
| | 238 | self.assertEqual(res.status_code, 302) |
| | 239 | self.assertRedirects(res, 'http://testserver/list/authors/') |
| | 240 | self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>']) |
| | 241 | |