Django在其每个端口分别为每个应用程序提供服务 [英] Django serving each app separately in each its port

查看:150
本文介绍了Django在其每个端口分别为每个应用程序提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django上运行了一个非常简单的项目(尚无模型),我需要执行以下操作:

I've got a very simple project running on Django (no models yet) and I need to do the following:

我创建了2个应用程序,'Ebony''Ivory',它们需要通过JSON消息相互通信(最初设计为在不同的计算机上运行,​​但到目前为止已经足够了).

I have created 2 apps, 'Ebony' and 'Ivory' that need to communicate with each other through JSON messages (originally designed to run on different machines but for now one is good enough).

问题在于Django Debug服务器只是在特定端口中运行的一个进程.我想做的是让每个'App'在同一服务器上侦听其自己的端口,如果可能,请在同一Django项目下侦听它的端口.这样的情况可能吗?如果是的话,我应该怎么做?

The problem is that the Django Debug server is just one process which runs in a specific port. What I want to do is make each 'App' listen to its own port on the same server and if possible under the same Django project. Is such a scenario possible? And if yes, how should I go about it?

预先感谢

推荐答案

这是可能的,但是您没有将其概念化的方式. Django应用程序是在给定Web服务器上运行的应用程序的一部分.因此,具有一个或多个应用程序的Django项目将作为一台Web服务器的一部分运行.

This is possible, but not the way you're conceptualizing it. A Django app is one part of what runs on a given web server. Thus a Django project, which has one or more apps, runs as a part of one web server.

解决方案是运行Django的多个实例.不确定调试服务器对您的工作效果如何.您可以单独运行每个服务器端口(通过为它提供一个参数,告诉它打开端口的位置),例如:

The solution is to run multiple instances of Django. Not sure how well this is going to work for you with the debug servers. You can run each server on its own port by giving it a parameter telling it where to open the port, for example:

./manage.py runserver 8000

在127.0.0.1:8000上运行调试服务器,并且

runs a debug server on 127.0.0.1:8000, and

./manage.py runserver 8080

在127.0.0.1:8080上运行另一个调试服务器.通常,这是在单独的外壳中完成的.

runs another debug server on 127.0.0.1:8080. Usually this is done in separate shells.

您将需要确保其中一个的INSTALLED_APPS设置中包含'Ebony',而另一个中的'Ivory'.您还需要找出某种方法来告诉每个实例如何连接到另一个实例(通常通过指定根URL).

You will need to make sure that the INSTALLED_APPS setting on one of these has 'Ebony' in it, and the other has 'Ivory'. You will also need to figure out some way to tell each instance how to connect to the other (usually by specifying a root URL).

也就是说,以后您将需要确定两个应用程序是否将共享同一数据库.如果是这样,请确保两台机器都可以使用它.如果不是,请确保每个settings.py中的DATABASES值都不同.如果您要共享数据库,则Django的网站框架可以为您提供帮助保持模型中的内容直接.

That said, later on you will need to figure out if your two apps will be sharing the same database. If so, make sure that both machines can get to it. If not, make sure the DATABASES value in settings.py is different for each one. If you're sharing the database, Django's sites framework can help you keep things straight in your models.

要使两者都在同一个项目中运行,您必须告诉Django要运行哪个项目.我更喜欢使用环境变量.这会将上面的runserver命令更改为:

To have both running from the same project, you have to tell Django which one to run. I prefer to use an environment variable. This changes the above runserver commands to:

SHARD=Ebony ./manage.py runserver 8000

SHARD=Ivory ./manage.py runserver 8080

在您的settings.py文件中,可以通过os.environ访问此变量.因此,例如,对于INSTALLED_APPS设置,每个分片具有不同的值,您可以编写类似以下内容的

In your settings.py file, this variable can be accessed through os.environ. So, for example, for the INSTALLED_APPS setting to have different values for each shard, you write something like:

SHARD = os.environ["SHARD"]

# Apps common to all shards go here.
LOCAL_APPS = [
    commonApp,
]

# Add apps specific to each shard.
if SHARD == "Ebony":
    LOCAL_APPS += [
        Ebony,
    ]
elif SHARD == "Ivory":
    LOCAL_APPS += [
        Ivory,
    ]

# Add them to the apps that aren't mine.
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.admin',
    # ... omitted for brevity ...
    'django_extensions',
    'south',
    'debug_toolbar',
) + LOCAL_APPS

通过在该文件中将SHARD定义为设置,可以避免必须让所有代码访问环境变量,并且可以将将SHARD设置为settings.py的逻辑限制在需要更改的情况下之后.如果需要,您的其他Python文件可以通过from django.conf.settings import SHARD进行设置.

By defining SHARD as a setting in this file, you avoid having to have all your code access the environment variable, and you confine the logic for setting SHARD to settings.py, in case you want to change it later. Your other Python files, if needed, can get the setting with from django.conf.settings import SHARD.

也可以使用类似的机制为每个分片赋予其自己的DATABASES设置.还有settings.py中的其他内容.

A similar mechanism can be used to give each shard its own DATABASES setting, too. And anything else in settings.py.

然后在您的urls.py文件中,使用该文件拉入应用程序的URL:

Then later in your urls.py file, you use that to pull in your apps' URLs:

from django.conf.urls import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'commonApp.views.get_homepage', name='home'),
    url(r'^login$', 'django.contrib.auth.views.login', name="login"),
    url(r'^logout$', 'django.contrib.auth.views.logout', 
        {"next_page": "/"}, name="logout"),
# Admin
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

# Auto-add the applications.
for app in settings.LOCAL_APPS:
    urlpatterns += patterns('',
        url(r'^{0}/'.format(app), include(app + '.urls', namespace=app)),
    )

这意味着您的应用程序需要它们自己的urls.py文件,并且您的应用程序URL名称以您的应用程序名称为前缀.因此,如果应用程序Ebony使用name="index"定义了网址格式,则可以使用{% url 'Ebony:index' %}在模板中获取该网址.

This means your apps need their own urls.py files, and your app URL names get prefixed with your app names. So if the app Ebony defines a URL pattern with name="index", you would get that URL in a template with {% url 'Ebony:index' %}.

这篇关于Django在其每个端口分别为每个应用程序提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆