简体   繁体   English

Django中间件:无法使urlredirect正常工作

[英]Django Middleware: Can't get urlredirect to work

So I'm very new to Django, but have completed the tutorials on (https://docs.djangoproject.com/en/1.4/intro/tutorial01/) several times and feel that I'm getting a pretty good handle on it. 因此,我是Django的新手,但是已经完成了(https://docs.djangoproject.com/en/1.4/intro/tutorial01/)上的教程几次,并觉得我已经掌握了相当不错的方法。

The next item I'd like to tackle is creating , installing and configuring middleware (more specifically i'm trying to make it so that when local host accesses the site that it pulls up fine and when someone other than local host pulls it up that it forwards to google.com or some other random site.) I'm mainly doing this for experience building at the request of my boss, so any help would be greatly appreciated! 我要解决的下一个项目是创建,安装和配置中间件(更具体地说,我正在尝试制作中间件,以便当本地主机访问该站点时,它可以正常运行,而当本地主机以外的其他人对其进行启动时, (它会转发到google.com或其他随机网站。)我主要是为了应老板的要求进行经验构建,因此,我们将不胜感激! :) :)

I've read the following sites, but can't seem to figure out what to do to get the url redirect to work. 我已经阅读了以下网站,但似乎无法弄清楚如何使url重定向起作用。

$https://docs.djangoproject.com/en/dev/topics/http/middleware/?from=olddocs $ https://docs.djangoproject.com/en/dev/topics/http/middleware/?from = olddocs

$https://docs.djangoproject.com/en/dev/ref/middleware/ $ https://docs.djangoproject.com/en/dev/ref/middleware/

$http://djangosnippets.org/snippets/510/ $ http://djangosnippets.org/snippets/510/

Code that I've picked up from the above site (www.djangosnippets.org) 我从上述网站(www.djangosnippets.org)提取的代码

import re

from django.http import HttpResponsePermanentRedirect
from django.conf import settings


class UrlRedirectMiddleware:
"""
This middleware lets you match a specific url and redirect the request to a
new url.

You keep a tuple of url regex pattern/url redirect tuples on your site
settings, example:

URL_REDIRECTS = (
    (r'www\.example\.com/hello/$', 'http://hello.example.com/'),
    (r'www\.example2\.com/$', 'http://www.example.com/example2/'),
)

"""
def process_request(self, request):
    host = request.META['HTTP_HOST'] + request.META['PATH_INFO']
    for url_pattern, redirect_url in settings.URL_REDIRECTS:
        regex = re.compile(url_pattern)
        if regex.match(host):
            return HttpResponsePermanentRedirect(redirect_url)

Add to your settings (assuming you are working with the development server): 添加到您的设置中(假设您正在使用开发服务器):

eg 例如

URL_REDIRECTS = (
    (r'127.0.0.1:8000/hello', 'http://www.google.com'),
)

Also add the UrlRedirectMiddleware python-path to your MIDDLEWARE_CLASSES in your settings. 还要在设置UrlRedirectMiddleware python-path添加到MIDDLEWARE_CLASSES

eg if your UrlRedirectMiddleware is defined in a module called my_middle.py inside an application called app 例如,如果您的UrlRedirectMiddleware是在名为app的应用app my_middle.py模块中定义的

add 'app.my_middle.UrlRedirectMiddleware' to your MIDDLEWARE_CLASSES 'app.my_middle.UrlRedirectMiddleware'添加到您的MIDDLEWARE_CLASSES

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

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