Getting response code 302 instead of 200 while testing a url

930 views
Skip to first unread message

Mannu Gupta

unread,
Jul 27, 2017, 4:56:04 PM7/27/17
to Django users
I have a django named blog and trying to test the url. The code for testing is :-

class PostTests(TestCase):

    def setUp(self):
        self.user = get_user_model().objects.create(username='some_user')
        self.title = "test-title"
        Post.objects.create(
            title=self.title, user=self.user,
            publish=datetime.date.today()
        )

    def test_post_list(self):
        response = self.client.get('/posts/search/')
        self.assertEqual(response.status_code, 200)

    def test_post_create(self):
        response = self.client.get('/posts/create/')
        self.assertEqual(response.status_code, 200)

    def test_post_update(self):
        response = self.client.get('/posts/test-title/update/')
        self.assertEqual(response.status_code, 200)

    def test_post_delete(self):
        response = self.client.get('/posts/test-title/delete/')
        self.assertEqual(response.status_code, 200)





The problem here is that my test cased passed for the first 2 functions i.e. "test_post_list" and "test_post_create" but now working for other two test functions i.e "test_post_update" and "test_post_delete". What might be the problem ?

The setup function that i made is supposed to create a object of model named "Post", then what might be the problem ?

James Schneider

unread,
Jul 28, 2017, 1:14:02 AM7/28/17
to django...@googlegroups.com

The problem here is that my test cased passed for the first 2 functions i.e. "test_post_list" and "test_post_create" but now working for other two test functions i.e "test_post_update" and "test_post_delete". What might be the problem ?

The setup function that i made is supposed to create a object of model named "Post", then what might be the problem ?

Actually,  that's probably a valid/correct response. In general, when sending a POST request, a redirect (the 302) is returned by a view to another landing page (where you would then receive a 200), where the successful message is displayed to the user. This is done to prevent the user from submitting the form multiple times by clicking the back button (the browser presents the "are you sure you want to send this data again?" message). 

The generic create/update views included with Django have this behavior. 

Without seeing the views, it's hard to tell for sure. 

-James 

Mannu Gupta

unread,
Jul 31, 2017, 7:40:08 PM7/31/17
to django...@googlegroups.com
Hi James,

Thanks for the reply!

As in the setup function for my tests i have already created a Post object then how just visiting a url i.e '/update/' and 'delete/' of that object can be a considered 'POST' request.

Also Is there any way to capture the inbetween changing process of status code i.e from 302 to 200 ? I have tried Postman but it was giving 200 staus code for visiting the 'update' url of the post that i had already created.

As you mentioned it is hard to debug without reading code, what part of code more should i send ?


Mannu

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/uZ1oGmHWWB8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWSCD9hK_cViYgFJuFOXFvC%2Bp1Wxz1oMFuHOySvhZYnAw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages