【问题标题】:Running my work webapp locally but cant access the correct site在本地运行我的工作 webapp 但无法访问正确的站点
【发布时间】:2019-05-14 13:13:26
【问题描述】:

我一直在尝试让 Django webapp 在本地运行。我所做的是设置我的debug = True 并基本上从我的测试服务器上携带我的settings.py 文件。我终于让我的python manage.py runserver [::]:8000 顺利运行而没有错误。我现在遇到的问题是我无法访问正确的想法。

抱歉,任何不正确的术语,但假设我的应用程序位于三个链接 www.webapp.com、www.buying.webapp.com 和 www. sell.webapp.com,其中 SITE_ID 为 1、2、3。我之前已经学会使用 localhost:8000 来访问 webapps,但那是我更简单的 webapps。

任何帮助将不胜感激!谢谢。

编辑: 我关注了DOMAINS_URLCONF,发现了这个

class SubdomainMiddleware:
    """ Make the subdomain publicly available to classes """

    def process_request(self, request):
        domain_parts = request.get_host().split('.')
        if (len(domain_parts) > 2):
            subdomain = domain_parts[0]
            if (subdomain.lower() == 'www'):
                subdomain = None
            domain = '.'.join(domain_parts[1:])
        else:
            subdomain = None
            domain = request.get_host()
#         if subdomain in settings.DOMAINS_URLCONF:
#             request.__setattr__('urlconf',settings.DOMAINS_URLCONF[subdomain])
        try:
            current_site = Site.objects.get(domain=request.get_host())
        except Site.DoesNotExist:
            current_site = Site.objects.get(id=settings.SITE_ID)

        request.current_site = current_site
        #settings.SITE_ID = current_site.id
        request.subdomain = subdomain
        request.domain = domain

【问题讨论】:

  • 这些子域上的路由是如何工作的?
  • 你的 webapp 是在 127.0.0.1:8000 还是 localhost:8000 上运行的?
  • @Sayse 看起来它们在我的 DOMAINS_URLCONF 下的 settings.py 中。 @Manumathew 当我尝试连接时,它们都会连接。

标签: python django localhost


【解决方案1】:

为此,您只需要在本地计算机上使用不同的域。这可以通过编辑您的本地 /etc/hosts 文件(在 Windows 上为 C:\Windows\System32\drivers\etc\hosts)来简单地实现。只需在其中添加这一行:

127.0.0.1     buying.x.localhost selling.x.localhost

更新:

查看您的中间件代码后,还有一个警告:用于购买和销售的本地域应至少包含 3 个部分。我刚刚在上面的域中间添加了.x,所以它应该被这个中间件正确解析。您可以根据需要构建这些 url,但它们必须至少包含 3 个部分,并且第一部分必须匹配您项目的一个子域。

完成此操作后,如果您在浏览器的地址栏中仅输入localhost:8000,您应该会看到主页,输入buying.x.localhost:8000 您将看到buying. 子域的内容,输入selling.x.localhost:8000你会看到selling.子域的内容。

【讨论】:

  • 嗯所以我关注了DOMAINS_URLCONF,它看起来不像被使用。我在编辑中找到了子域的中间件。是不是很眼熟
  • 我认为它应该在不接触DOMAINS_URLCONF的情况下工作。
  • 这是一个可怕的问题,但是如果我同时拥有这两个问题,那么我怎么知道我要访问哪个域?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 2011-08-23
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多