注意:由于过度简化导致示例代码错误,因此重写了问题。
要对此进行测试,请确保您的主机文件中有以下设置:
test.local 127.0.0.1
subdomain.test.local 127.0.0.1
在 settings.py 中,将 cookie 设置为跨子域工作:
SESSION_COOKIE_DOMAIN = '.test.local'
然后我有一个看起来像这样的中间件:
from django.contrib import messages
from django.http import HttpResponseRedirect
class AuthorizeAccount:
def process_request(self, request):
if request.get_host() != 'test.local:8000':
messages.error(request, u'No subdomain!')
return HttpResponseRedirect('http://test.local:8000/')
return None
我的模板包含:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
您可以通过访问您的开发服务器上的http://test.local:8000(不希望有消息)和访问http://subdomain.test.local:8000(希望重定向到http://test .local:8000和一条消息)。
我确实收到了重定向,但我没有收到消息。我错过了什么还是这是一个错误?