0

I am probably staring it right in the face here. But does Django have a way of actually checking what the HTTP Status code is. I'm basically interested if it's the 302 redirect status code. But no matter what I try and search for I'm only getting HTTPResponse() which is no good to me.

5
  • Do you mean in a unittest or what? Response.status_code (or whatever you name your response variable) should get it. Commented May 2, 2014 at 21:05
  • No not in a unittest, this is what I keep getting when I search google for this situation. I purely just want to get the status code within a view. Commented May 2, 2014 at 21:06
  • I don't think there is a status code for a request, only for a response. Why do you need it in the view? Commented May 2, 2014 at 21:15
  • I'm trying to see if a view is being accessed via a redirect or if they have directly gone to the page. Commented May 2, 2014 at 21:16
  • Do you know where it would be redirected from? You could do request.META.get('HTTP_REFERER', None) to see where it was referred from (if anywhere). Commented May 2, 2014 at 21:22

1 Answer 1

1

Your question unfortunately doesn't make any sense. The reason why you don't get any Google results for this is that there's simply no such thing as a status code for a request. The status code is what the server sends back to the client as a result of the request: it's unavoidably part of the response, not the request.

I suspect however that this is an XY problem. What, exactly, are you trying to achieve? If you need some data to be preserved through a redirect, then you probably want to store it in the session.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to check in the view if the view has been accessed directly or as a redirect, as we only wish the view to be access if it's been redirected to.
As I say, then, you should store something in the session in the originating view, then check it in the new view: if it's there, you can be sure it's a redirect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.