Django的ALLOWED

编程入门 行业动态 更新时间:2024-10-28 06:30:40
本文介绍了Django的ALLOWED_HOSTS接受通过Apache的本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我服与Apache一个Django应用程序。在Django的settings.py我有 DEBUG =假,所以我不得不让一些主机,如: ALLOWED_HOSTS = ['.dyndns', 'localhost'的] 。这工作得很好,但是我想有通过其内部的IP地址在本地网络上访问服务器,以及像: 192.168.0.X 或 127.0.0.1 ,等我怎么可以定义 192。* 或 127。* 在 ALLOWED_HOSTS ,如果我想用 ALLOWED_HOSTS =避免开放接入完全['*'] ?

I'm serving a Django app with Apache. In Django's settings.py I have DEBUG = False, therefore I had to allow some hosts, like: ALLOWED_HOSTS = ['.dyndns', 'localhost']. This works fine, however I would like to have the server accessible on the local network via its internal IP address as well, like: 192.168.0.x, or 127.0.0.1, etc. How could I define 192.* or 127.* in ALLOWED_HOSTS, if I'd like to avoid opening up the access entirely by ALLOWED_HOSTS = ['*']?

推荐答案

继@rnevius的建议,并根据从@AlvaroAV中的如何设置自定义的中间件在Django ,我已经成功与该中间件解决:

Following the recommendation from @rnevius, and based on the guidelines from @AlvaroAV in how to setup custom middleware in django, I've managed to solve with this middleware:

from django.http import HttpResponseForbidden class FilterHostMiddleware(object): def process_request(self, request): allowed_hosts = ['127.0.0.1', 'localhost'] # specify complete host names here host = request.META.get('HTTP_HOST') if host[len(host)-10:] == 'dyndns': # if the host ends with dyndns then add to the allowed hosts allowed_hosts.append(host) elif host[:7] == '192.168': # if the host starts with 192.168 then add to the allowed hosts allowed_hosts.append(host) if host not in allowed_hosts: raise HttpResponseForbidden return None

和 settings.py 设置 ALLOWED_HOSTS = ['*'] 不再开辟了在所有主机不受控制的方式。

and setting ALLOWED_HOSTS = ['*'] in settings.py no longer opens up for all hosts in an uncontrolled way.

谢谢你们! :)

更多推荐

Django的ALLOWED

本文发布于:2023-07-16 05:55:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1118575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   ALLOWED

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!