Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
http://www.website.com http://www.website.eu
How to get current domain in django views without com or eu?
com
eu
http://www.website
您必须做一些体力劳动才能获得域名。
使用request.META['HTTP_HOST']会给你域,但它通常是一个坏主意,因为它可以被欺骗(即伪造)。
request.META['HTTP_HOST']
但无论如何,让我们玩吧!我将使用 Python 切片语法。
def domain(domain): return domain[:domain.rfind('.')] >> domain(request.META['HTTP_HOST']) #http://www.website.whatever >> "http://www.website"