My django.test Client returns a response with status code, 200, when I made a post request instead of redirecting with a status code, 302.
I am using Django 2.2.4 with Python 3.7.3.
No login is required (as addressed by Why does Django Redirect Test Fail?) neither does setting the follow and secure parameters to True (as addressed by Django test client POST command not registering through 301 redirect)
I noticed that the same problem affects my update view. It doesn't redirect when I call the Django Test Client with a put method. (I didn't include this code to avoid verbosity).
Finally, I noticed that the page redirect works when I python manage.py runserver
and then visit the page to create a new Book instance.
Here's the test:
from django.test import TestCase, Client
from django.urls import reverse
...
class TestDashboardViews(TestCase):
def setUp(self):
self.client = Client()
self.book_list_url = reverse("dashboard-home")
self.book_create_url = reverse("dashboard-upload-book")
self.another_demo_book_kwargs = {
"title": "Demo Title",
"author": "Demo Author",
"subject": "Another Demo Subject",
"details": "Another Demo Details",
"file": "path/to/another/demo/file"
}
...
def test_book_create_view_using_post_method(self):
response = self.client.post(self.book_create_url, self.another_demo_book_kwargs)
self.assertRedirects(response, self.book_list_url)
...
Here's the affected view:
class BookCreateView(CreateView):
model = Book
fields = ['title', 'author', 'subject', 'details', 'file']
success_url = reverse_lazy("dashboard-home")
Here's the model:
from django.db import models
from django.utils.text import slugify
class Book(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField()
author = models.CharField(max_length=100)
subject = models.CharField(max_length=50)
details = models.CharField(max_length=255)
def author_title_path(self, filename):
return "files/{author}/{filename}".format(author=slugify(self.author), filename=filename)
file = models.FileField(upload_to=author_title_path)
def save(self, *args, **kwargs):
title_by_author = "{title} by {author}".format(title=self.title, author=self.author)
self.slug = slugify(title_by_author)
super().save(*args, **kwargs)
and here are my urls:
from django.urls import path
from dashboard.views import BookListView, BookCreateView, BookUpdateView, BookDeleteView
urlpatterns = [
path("", BookListView.as_view(), name="dashboard-home"),
path("add_book/", BookCreateView.as_view(), name="dashboard-upload-book"),
path("edit_book/<slug>/", BookUpdateView.as_view(), name="dashboard-edit-book"),
path("delete_book/<slug>/", BookDeleteView.as_view(), name="dashboard-delete-book")
]
My test fails with this error:
...
AssertionError: 200 != 302 : Response didn't redirect as expected: Response code was 200 (expected 302)
...
Please, help.