40π
β
It seems that if you POST
to a form, you have to post all required fields, not just the ones you are updating β even if the required field of the underlying model already has a value. Also, the status code returned upon a successful update is 302 βFoundβ, not 200 βOKβ. So the following test passes:
class BookUpdateTest(TestCase):
def test_update_book(self):
book = Book.objects.create(title='The Catcher in the Rye')
response = self.client.post(
reverse('book-update', kwargs={'pk': book.id}),
{'title': 'The Catcher in the Rye', 'author': 'J.D. Salinger'})
self.assertEqual(response.status_code, 302)
book.refresh_from_db()
self.assertEqual(book.author, 'J.D. Salinger')
π€Kurt Peek
1π
In Django 3.2, you can find here the canonical solution:
https://docs.djangoproject.com/fr/3.2/ref/urlresolvers/
In a test, you just need to:
- create the objects with the values included in the slug,
- and then, reverse it with these arguments:
self.topic = Book.objects.create(slug="test-update")
self.response = self.client.get(reverse('book_update', args=[self.topic.slug]))
π€Fabrice JaouΓ«n
- [Django]-Django related_name for field clashes
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-Django β getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
0π
TestCase
has self.assertContains(response, el, html=True)
. html= True
will render the TemplateResponse
for you.
π€alias51
- [Django]-Exclude fields in Django admin for users other than superuser
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
Source:stackexchange.com