[英]How can i redirect from one domain to another in django app?
Normally i would do it with .htaccess
but django doesn't have it.通常我会用.htaccess
来做,但 django 没有。
So what is the best way and what is the code for it to redirect from www.olddomain.com to www.newdomain.com?那么从 www.olddomain.com 重定向到 www.newdomain.com 的最佳方法是什么?代码是什么?
NOTE: we are not using Apache, but Gunicorn注意:我们使用的不是 Apache,而是 Gunicorn
thanx!谢谢!
The best way to do this is still with your web server rather than Django.最好的方法仍然是使用您的 Web 服务器而不是 Django。 This will be much quicker and more efficient than doing it with Django.这将比使用 Django 更快、更有效。
Check out this question for more info.查看此问题以获取更多信息。
UPDATE更新
If you really want to do it within django then edit your url conf file (which manages django's url dispatcher ) to include the following at the top -如果您真的想在 django 中执行此操作,请编辑您的 url conf 文件(管理django 的 url 调度程序)以在顶部包含以下内容 -
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
(r'^.*$', redirect_to, {'url': 'http://www.newdomain.com'}),
)
For more info check out the documentation .有关更多信息,请查看文档。
import urlparse
from django.http import HttpResponseRedirect
domain = request.GET['domain']
destination = reverse('variable_response',args=['Successful'])
full_address = urlparse.urljoin(domain, destination)
return HttpResponseRedirect(full_address)
I ended up doing it using heroku and spinning up 1 web dyno (which is free).我最终使用 heroku 并启动了 1 个 web dyno(这是免费的)。
#views.py
def redirect(request):
return render_to_response('redirect.html')
#redirect.html
<html>
<head>
<title>Blah</title>
<meta http-equiv="refresh" content="1;url=http://www.example.com">
</head>
<body>
<p>
Redirecting to our main site. If you're not redirected within a couple of seconds, click here:<br />
<a href="http://www.example.com">example.com</a>
</p>
</body>
</html>
Simple as that.就那么简单。 Can find the same example here .可以在此处找到相同的示例。
An alternative to catherine answer, updated to Python 3 is:更新到 Python 3 的凯瑟琳答案的替代方法是:
from django.contrib.sites.shortcuts import get_current_site
from urllib.parse import urljoin
from django.http import HttpResponseRedirect
NEW_DOMAIN = 'www.newdomain.com'
Put in each view
:放入每个view
:
def myView(request, my_id):
if request.META['HTTP_HOST'] != NEW_DOMAIN:
# remove the args if not needed
destination = reverse('url_tag', args=[my_id])
full_address = urljoin(DOMAIN, str(destination))
return HttpResponseRedirect(full_address)
# your view here
The url_tag
is the one defined in urlpatterns
. url_tag
是在urlpatterns
定义的urlpatterns
。
i had the same problem so i wrote this and it worked perfectly for me, maybe someone else needs it too:我遇到了同样的问题,所以我写了这个,它对我来说非常有效,也许其他人也需要它:
urlpatterns += [ # redirect to media server with same path
url(r'^media/', redirectMedia),
]
and using this function to redirect:并使用此函数重定向:
from urllib.request import urlopen
from django.http import HttpResponse
def redirectMedia(request):
x = urlopen("http://www.newdomain.com" + request.path)
return HttpResponse(x.read())
enjoy it!好好享受!
For Django >= 2.0
, the easier solution is to use RedirectView对于 Django >= 2.0
,更简单的解决方案是使用RedirectView
For example in urls.py
:例如在urls.py
:
from django.views.generic.base import RedirectView
urlpatterns = [
path('my_ext_uri', RedirectView.as_view(url='https://YOUR_EXTERNAL_URL')),
]
[Side note] [边注]
As mentioned in Aidan's answer , it would be better to redirect requests which will be handled by different services at web server gateway, rather than at (Python/Django) application server.正如 Aidan 的回答中提到的,最好重定向将由 Web 服务器网关上的不同服务处理的请求,而不是(Python/Django)应用程序服务器。
I ended with simple solution:我以简单的解决方案结束:
return HttpResponse(f"<script>location.replace('https://example.com/');</script>")
It works if user don't disable scripts in webbrowser如果用户不禁用 webbrowser 中的脚本,它会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.