How to show the visitor a moved web page AND return a 301 redirect HTTP response status code in Django?
When a webpage has moved to a new location, how do I show the moved web page AND return a 301 permanent redirect HTTP response status code in Django?
Asked by: Madaline414 | Posted: 28-01-2022
Answer 1
from django import http
return http.HttpResponsePermanentRedirect('/yournewpage.html')
the browser will get the 301, and go to /yournewpage.html
as expected. the other answer is technically correct, in that python is not handling the redirection per se, the browser is. this is what's happening under the hood:
Browser Python HTTP
-------------------> GET /youroldpage.html HTTP/1.1
<------------------- HTTP/1.1 301 Moved Permanently
Location: /yournewpage.html
-------------------> GET /yournewpage.html HTTP/1.1
Answered by: Patrick562 | Posted: 01-03-2022
Answer 2
You can't.
301 is an HTTP return code that is directly acted upon by the browser. Many sites handle these two issues by first sending the user to a redirect-er page that tells the user about the change and then X seconds later sends them to the new page. But the redirect-er page must have a 200 code.
One small variant is to detect search engine spiders (by IP and/or user agent) and give them the 301. That way the search results point to your new page.
Answered by: Jared277 | Posted: 01-03-2022Similar questions
python - response redirect to a different page when 405 raise
I have a view function which I want to use only request method is POST. I got to know about require_POST decorator here, but what I want is that if method is not POST then redirect user to different view using HttpResponseRedirect. How can I do this ?
from django....
python - Reading urls from CSV, Get HTTP Response Status, Check for Redirect, Store in new CSV
I have a csv file that has a bunch of urls in it. I am working on a script that does the following:
Read urls in from the csv
Pass url list to request.get()
and then
Get initial http status along with the redirected url and http status
My code works fine when I only pass in a single url and don't try and read the urls from another csv. However at scale it makes the most sen...
python - Flask can't redirect me to home page, it just gives me the html code as response
I'm building a login with flask and flask-login, and the authentication works perfectly. When a user is authenticated, he should be automatically redirected to the home page (which is unavailable for a not-logged user). The issue is that flask.redirect function just returns me the html code of the home page instead of redirecting to that page. I see the html code in the response object inside the browser conso...
python - AWS Cognito login 'code' vs 'token' response type (# symbol in redirect URL)
I'm trying to learn how to use AWS Cognito, and I'm confused about the different 'response_type' options when integrating my (test) client app with the Cognito login UI. If I select 'token' rather than 'code', the redirect URL generated by Cognito following successful login has a '#' symbol before the arguments, which prevents my test app (python, Flask) from parsing the arguments.
I'm wondering if someone can expl...
python - how do I make my command line http client receive a redirect response from a server and then make it send another request to the new location?
So I am trying to solve the part 1 of the assignment given here:
https://stevetarzia.com/teaching/340/projects/project-1.html
I have written a python program that sends a request and fetches the HTML of the page but I don't have any idea about how to handle redirects.
So far what I have tried is to check if response ...
python - How to produce a 303 Http Response in Django?
Last couple of days we were discussing at another question the best to manage randomness in a RESTful way; today I went to play a little bit with some ideas in Django only to find that there is no easy standard way of returning a 303 response (nor a 300 one, btw), that is, there doesn't seem to exist an HttpResponseSeeOther inside...
python - What does the LDAP response tuple (97, []) mean?
I am using python-ldap to try to authenticate against an existing Active Directory, and when I use the following code:
import ldap
l = ldap.initialize('LDAP://example.com')
m = l.simple_bind_s(username@example.com,password)
I get the following back:
print m
(97, [])
What does the 97 and empty list signify coming from a Microsoft Active Directory server?
python - caching issues in MySQL response with MySQLdb in Django
I use MySQL with MySQLdb module in Python, in Django.
I'm running in autocommit mode in this case (and Django's transaction.is_managed() actually returns False).
I have several processes interacting with the database.
One process fetches all Task models with Task.objects.all()
Then another process adds a Task model (I can see it in a database management application).
If I call Task....
python - Hex data from socket, process and response
Lets put it in parts.
I got a socket receiving data OK and I got it in the \x31\x31\x31 format.
I know that I can get the same number, ripping the \x with something like
for i in data: print hex(ord(i))
so I got 31 in each case.
But if I want to add 1 to the data (so it shall be "32 32 32")to send it as response, ...
python - Django - flush response?
I am sending an AJAX request to a Django view that can potentially take a lot of time. It goes through some well-defined steps, however, so I would like to print status indicators to the user letting it know when it is finished doing a certain thing and has moved on to the next.
If I was using PHP it might look like this, using the flush function:
python - Is it possible to peek at the data in a urllib2 response?
I need to detect character encoding in HTTP responses. To do this I look at the headers, then if it's not set in the content-type header I have to peek at the response and look for a "<meta http-equiv='content-type'>" header. I'd like to be able to write a function that looks and works something like this:
response = urllib2.urlopen("http://www.example.com/")
encoding = detect_html_enco...
url - Python urllib2 Response header
I'm trying to extract the response header of a URL request. When I use firebug to analyze the response output of a URL request, it returns:
Content-Type text/html
However when I use the python code:
urllib2.urlopen(URL).info()
the resulting output returns:
Content-Type: video/x-flv
I am new to python, and to web programmin...
python - Sending stdout as response from CGI spawned program
I'm trying to compose a .zip file in a CGI program and send that as the content response. I'm getting stuck in that whenever I spawn a program that prints to stdout, that somehow doesn't get accepted by Apache. It seems to be something to do with spawning a program that writes to stdout. The snippet below reproduces this problem. I always get the following error form Apache:
malformed header from scrip...
urllib - Parsing Python Response using httplib
After connecting to a socket and capturing the response using .read() how do I parse the input stream and read lines?
I see the data is returned without any CRLF
<html><head><title>Apache Tomcat/6.0.16 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;...
python - Getting INVALID response from PayPal's Sandbox IPN
I am trying to implement a simple online payment system using PayPal, however I have tried everything I know and am still getting an INVALID response.
I know it's nothing too simple, because I get a VERIFIED response when using the IPN simulator. I have tried putting the items into a dict first, I have tried fixing the encoding, and still nothing. PayPal says the reasons for an INVALID response could be:
...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python