In my Django app, I need to get the host name from the referrer in request.META.get('HTTP_REFERER')
along with its protocol so that from URLs like:
https://docs.google.com/spreadsheet/ccc?key=blah-blah-blah-blah#gid=1
https://mcmap.net/q/128757/-c-program-works-from-cmd-prompt-but-not-run-separately
http://www.example.com
https://www.other-domain.example/whatever/blah/blah/?v1=0&v2=blah+blah
I should get:
https://docs.google.com/
https://stackoverflow.com/
http://www.example.com
https://www.other-domain.example/
I looked over other related questions and found about urlparse, but that didn't do the trick since
>>> urlparse(request.META.get('HTTP_REFERER')).hostname
'docs.google.com'
/
to the third examplehttp://www.domain.com
, but I think this might be a shortcoming of the question, not of the answer. – Tobytobye