Django settings.PREPEND_WWW与test client 的配合失误

功能:使用django.test.client.Client 来访问某个 url 以确定页面是否正常。
问题:本地运行良好,部署到服务器之后所有的链接都被301,但是shell在服务器仍然正常。
原因:
省略若干字……
跟踪log
2010-04-02 14:34:31,625 INFO utils.verify_internal Line:120 response: Date: Fri, 02 Apr 2010 14:34:31 GMT
Content-Length: 0
ETag: "d41d8cd98f00b204e9800998ecf8427e"
Content-Type: text/html; charset=utf-8
Location: http://www.testserver/ #这里的location被加上了www,事后证明就是这个造成了301。

1. PREPEND_WWW = sys.argv[0] != 'manage.py'
2. 在服务器上采用wsgi部署,因此没有manage.py,所以PREPEND_WWW = True

解决:

class LinkCheckHandler(ClientHandler): '''remove reversion.middleware.RevisionMiddleware for test Client''' def load_middleware(self): super(LinkCheckHandler, self).load_middleware() new_request_middleware = [] #############################_request_middleware################################# for method in self._request_middleware: if not method.__str__().count('django.middleware.common.CommonMiddleware'): new_request_middleware.append(method) else: log.info('removing %s' % method) self._request_middleware = new_request_middleware ##其他三个middleware一样处理... c = Client() c.handler = LinkCheckHandler()

你可能感兴趣的:(pyryday,Django,实践,django,服务器,etag,shell,2010,date)