• 用django实现redirect的几种方法总结


    用django开发web应用, 经常会遇到从一个旧的url转向一个新的url。这种隐射也许有规则,也许没有。但都是为了实现业务的需要。总体说来,有如下几种方法实现 django的 redirect。
    1. 在url 中配置 redirect_to 或者 RedirectView(django 1.3 版本以上)
    2. 在view 中 通过 HttpResponseRedirect 实现 redirect

    3. 利用 django 的 redirects app实现

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

    [python]view plain copy
     
     print?
    1. from django.views.generic.simple import redirect_to  
    2. urlpatterns = patterns('',  
    3.     (r'^one/$', redirect_to, {'url': '/another/'}),  
    4. )  
    [python]view plain copy
     
     print?
    1. from django.views.generic import RedirectView  
    2. urlpatterns = patterns('',  
    3.     (r'^one/$', RedirectView.as_view(url='/another/')),  
    4. )  



    2. 在view 中 通过 HttpResponseRedirect 实现 redirect 

    [python]view plain copy
     
     print?
    1. from django.http import HttpResponseRedirect  
    2.   
    3. def myview(request):  
    4.     ...  
    5.     return HttpResponseRedirect("/path/")  



    3. 利用 django 的 redirects app实现 
    1. 在settings.py 中  增加 'django.contrib.redirects' 到你的 INSTALLED_APPS 设置.
    2. 增加 'django.contrib.redirects.middleware.RedirectFallbackMiddleware' 到你的MIDDLEWARE_CLASSES 设置中.
    3. 运行 manage.py syncdb. 创建 django_redirect 这个表,包含了 site_id, old_path and new_path 字段.


    主要工作是 RedirectFallbackMiddleware  完成的,如果 django  发现了404 错误,这时候,就会进django_redirect 去查找,有没有匹配的URL 。如果有匹配且新的RUL不为空则自动转向新的URL,如果新的URL为空,则返回410. 如果没有匹配,仍然按原来的错误返回。


    注意,这种仅仅处理 404 相关错误,而不是 500 错误的。
    增加删除 django_redirect 表呢?

    [python]view plain copy
     
     print?
    1. from django.db import models  
    2. from django.contrib.sites.models import Site  
    3. from django.utils.translation import ugettext_lazy as _  
    4. from django.utils.encoding import python_2_unicode_compatible  
    5.  
    6. @python_2_unicode_compatible  
    7. class Redirect(models.Model):  
    8.     site = models.ForeignKey(Site)  
    9.     old_path = models.CharField(_('redirect from'), max_length=200, db_index=True,  
    10.         help_text=_("This should be an absolute path, excluding the domain name. Example: '/events/search/'."))  
    11.     new_path = models.CharField(_('redirect to'), max_length=200, blank=True,  
    12.         help_text=_("This can be either an absolute path (as above) or a full URL starting with 'http://'."))  
    13.   
    14.     class Meta:  
    15.         verbose_name = _('redirect')  
    16.         verbose_name_plural = _('redirects')  
    17.         db_table = 'django_redirect'  
    18.         unique_together=(('site', 'old_path'),)  
    19.         ordering = ('old_path',)  
    20.   
    21.     def __str__(self):  
    22.         return "%s ---> %s" % (self.old_path, self.new_path)  



    采用类似如上的MODEL ,另外用DJANGO相关ORM 就可以实现save,delete了。

    以上三种方法都可以实现 django redirect,其实最常用的,是第一种与第二种,第三种方法很少用。 

  • 相关阅读:
    51nod1381 硬币游戏
    51nod1381 硬币游戏
    51nod1384 全排列
    LOJ P10130 点的距离 题解
    POJ P1985 Cow Marathon 题解
    求树的直径(两种方法)
    洛谷 P3518 [POI2011] SEJ-Strongbox 题解
    洛谷 UVA12101 Prime Path 题解
    POJ P2251 Dungeon Master 题解
    POJ P3009 Curling 2.0 题解
  • 原文地址:https://www.cnblogs.com/wumingxiaoyao/p/7110131.html
Copyright © 2020-2023  润新知