国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

詳解如何用django實(shí)現(xiàn)redirect的幾種方法總結(jié)

2020-01-04 14:00:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

用django開發(fā)web應(yīng)用, 經(jīng)常會(huì)遇到從一個(gè)舊的url轉(zhuǎn)向一個(gè)新的url。這種隱射也許有規(guī)則,也許沒(méi)有。但都是為了實(shí)現(xiàn)業(yè)務(wù)的需要。總體說(shuō)來(lái),有如下幾種方法實(shí)現(xiàn) django的 redirect。

1. 在url 中配置 redirect_to 或者 RedirectView(django 1.3 版本以上)
2. 在view 中 通過(guò) HttpResponseRedirect 實(shí)現(xiàn) redirect
3. 利用 django 的 redirects app實(shí)現(xiàn)

1 在url 中配置 redirect_to 或者 RedirectView(django 1.3 版本以上)

from django.views.generic.simple import redirect_tourlpatterns = patterns('',  (r'^one/$', redirect_to, {'url': '/another/'}),)from django.views.generic import RedirectViewurlpatterns = patterns('',  (r'^one/$', RedirectView.as_view(url='/another/')),)

2. 在view 中 通過(guò) HttpResponseRedirect 實(shí)現(xiàn) redirect

from django.http import HttpResponseRedirect def myview(request):  ...  return HttpResponseRedirect("/path/")

3. 利用 django 的 redirects app實(shí)現(xiàn) 

1. 在settings.py 中  增加 'django.contrib.redirects' 到你的 INSTALLED_APPS 設(shè)置.
2. 增加 'django.contrib.redirects.middleware.RedirectFallbackMiddleware' 到你的MIDDLEWARE_CLASSES 設(shè)置中.
3. 運(yùn)行 manage.py syncdb. 創(chuàng)建 django_redirect 這個(gè)表,包含了 site_id, old_path and new_path 字段.

主要工作是 RedirectFallbackMiddleware  完成的,如果 django  發(fā)現(xiàn)了404 錯(cuò)誤,這時(shí)候,就會(huì)進(jìn)django_redirect 去查找,有沒(méi)有匹配的URL 。如果有匹配且新的RUL不為空則自動(dòng)轉(zhuǎn)向新的URL,如果新的URL為空,則返回410. 如果沒(méi)有匹配,仍然按原來(lái)的錯(cuò)誤返回。

注意,這種僅僅處理 404 相關(guān)錯(cuò)誤,而不是 500 錯(cuò)誤的。

增加刪除 django_redirect 表呢?

from django.db import modelsfrom django.contrib.sites.models import Sitefrom django.utils.translation import ugettext_lazy as _from django.utils.encoding import python_2_unicode_compatible @python_2_unicode_compatibleclass Redirect(models.Model):  site = models.ForeignKey(Site)  old_path = models.CharField(_('redirect from'), max_length=200, db_index=True,    help_text=_("This should be an absolute path, excluding the domain name. Example: '/events/search/'."))  new_path = models.CharField(_('redirect to'), max_length=200, blank=True,    help_text=_("This can be either an absolute path (as above) or a full URL starting with 'http://'."))   class Meta:    verbose_name = _('redirect')    verbose_name_plural = _('redirects')    db_table = 'django_redirect'    unique_together=(('site', 'old_path'),)    ordering = ('old_path',)   def __str__(self):    return "%s ---> %s" % (self.old_path, self.new_path)

采用類似如上的MODEL ,另外用DJANGO相關(guān)ORM 就可以實(shí)現(xiàn)save,delete了。

以上三種方法都可以實(shí)現(xiàn) django redirect,其實(shí)最常用的,是第一種與第二種,第三種方法很少用。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江达县| 陆川县| 湟源县| 靖西县| 砀山县| 高碑店市| 石景山区| 恩施市| 信宜市| 静安区| 蒙山县| 宣威市| 井研县| 海南省| 花垣县| 成武县| 滨海县| 台东市| 花莲市| 浑源县| 东乌珠穆沁旗| 铁岭市| 平邑县| 黄大仙区| 冀州市| 霍邱县| 任丘市| 盖州市| 宝山区| 石河子市| 安新县| 福州市| 翁牛特旗| 苏尼特右旗| 大足县| 阜新市| 突泉县| 凤阳县| 樟树市| 德保县| 平湖市|