简体   繁体   English

Django URL 重定向

[英]Django URL Redirect

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 301 or 302 .就目前而言,最后一个条目将所有“其他”流量发送到主页,但我想通过HTTP 301302重定向。

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 url in the <url_to_home_view> you need to actually specify the url.请注意如何为url<url_to_home_view>实际上你需要指定的URL。

permanent=False will return HTTP 302, while permanent=True will 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() .在 Django 2+ 中, url()已被弃用并由re_path()取代。 Usage is exactly the same as url() with regular expressions.用法与带有正则表达式的url()完全相同。 For replacements without the need of regular expression, use path() .对于不需要正则表达式的替换,使用path()

from django.urls import re_path

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

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 ,它有点不干燥,并且可以确保您更改您的 url,您也不必更改重定向。

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)。

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')),
]

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"),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM