Python Django URL 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14959217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:57:37  来源:igfitidea点击:

Django URL Redirect

pythondjangodjango-urls

提问by felix001

How can I redirect traffic that doesn't match any of my other URLs back to the home page?

如何将与我的任何其他 URL 都不匹配的流量重定向回主页?

urls.py:

网址.py:

urlpatterns = patterns('',
    url(r'^$', 'macmonster.views.home'),
    #url(r'^macmon_home$', 'macmonster.views.home'),
    url(r'^macmon_output/$', 'macmonster.views.output'),
    url(r'^macmon_about/$', 'macmonster.views.about'),
    url(r'^.*$',  'macmonster.views.home'),
)

As it stands, the last entry sends all "other" traffic to the home page but I want to redirect via either an HTTP 301or 302.

就目前而言,最后一个条目将所有“其他”流量发送到主页,但我想通过HTTP 301302重定向。

采纳答案by dmg

You can try the Class Based View called RedirectView

您可以尝试称为基于类的视图 RedirectView

from django.views.generic.base import RedirectView

urlpatterns = patterns('',
    url(r'^$', 'macmonster.views.home'),
    #url(r'^macmon_home$', 'macmonster.views.home'),
    url(r'^macmon_output/$', 'macmonster.views.output'),
    url(r'^macmon_about/$', 'macmonster.views.about'),
    url(r'^.*$', RedirectView.as_view(url='<url_to_home_view>', permanent=False), name='index')
)

Notice how as urlin the <url_to_home_view>you need to actually specify the url.

请注意如何为url<url_to_home_view>您需要实际指定的URL。

permanent=Falsewill return HTTP 302, while permanent=Truewill return HTTP 301.

permanent=False将返回 HTTP 302,而permanent=True将返回 HTTP 301。

Alternatively you can use django.shortcuts.redirect

或者你可以使用 django.shortcuts.redirect

Update for Django 2+ versions

更新 Django 2+ 版本

With Django 2+, url()is deprecated and replaced by re_path(). Usage is exactly the same as url()with regular expressions. For replacements without the need of regular expression, use path().

在 Django 2+ 中,url()已弃用并由re_path(). 用法url()与正则表达式完全相同。对于不需要正则表达式的替换,使用path().

from django.urls import re_path

re_path(r'^.*$', RedirectView.as_view(url='<url_to_home_view>', permanent=False), name='index')

回答by Blaine Garrett

If you are stuck on django 1.2 like I am and RedirectView doesn't exist, another route-centric way to add the redirect mapping is using:

如果你像我一样被困在 django 1.2 并且 RedirectView 不存在,另一种添加重定向映射的以路由为中心的方法是使用:

(r'^match_rules/$', 'django.views.generic.simple.redirect_to', {'url': '/new_url'}),  

You can also re-route everything on a match. This is useful when changing the folder of an app but wanting to preserve bookmarks:

您还可以重新路由比赛中的所有内容。这在更改应用程序的文件夹但想要保留书签时很有用:

(r'^match_folder/(?P<path>.*)', 'django.views.generic.simple.redirect_to', {'url': '/new_folder/%(path)s'}),  

This is preferable to django.shortcuts.redirect if you are only trying to modify your url routing and do not have access to .htaccess, etc (I'm on Appengine and app.yaml doesn't allow url redirection at that level like an .htaccess).

如果您只是想修改您的 url 路由并且无权访问 .htaccess 等,这比 django.shortcuts.redirect 更可取(我在 Appengine 上,app.yaml 不允许在该级别进行 url 重定向,例如.htaccess)。

回答by sirFunkenstine

Another way of doing it is using HttpResponsePermanentRedirect like so:

另一种方法是使用 HttpResponsePermanentRedirect ,如下所示:

In view.py

在视图.py

def url_redirect(request):
    return HttpResponsePermanentRedirect("/new_url/")

In the url.py

在 url.py

url(r'^old_url/$', "website.views.url_redirect", name="url-redirect"),

回答by KhoPhi

In Django 1.8, this is how I did mine.

在 Django 1.8 中,我就是这样做的。

from django.views.generic.base import RedirectView

url(r'^$', views.comingSoon, name='homepage'),
# whatever urls you might have in here
# make sure the 'catch-all' url is placed last
url(r'^.*$', RedirectView.as_view(pattern_name='homepage', permanent=False))

Instead of using url, you can use the pattern_name, which is a bit un-DRY, and will ensure you change your url, you don't have to change the redirect too.

url您可以使用pattern_name,而不是使用,这有点不干,并且将确保您更改您的网址,您也不必更改重定向。

回答by bombs

The other methods work fine, but you can also use the good old django.shortcut.redirect.

其他方法工作正常,但您也可以使用旧的django.shortcut.redirect.

The code below was taken from this answer.

下面的代码取自这个答案

In Django 2.x:

在 Django 2.x 中:

from django.shortcuts import redirect
from django.urls import path, include

urlpatterns = [
    # this example uses named URL 'hola-home' from app named hola
    # for more redirect's usage options: https://docs.djangoproject.com/en/2.1/topics/http/shortcuts/
    path('', lambda request: redirect('hola/', permanent=True)),
    path('hola/', include('hola.urls')),
]