我们可以将两个连接到不同数据库的Django / python应用程序组合成一个集成的应用程序 [英] Can we combine two Django/python apps which connect to different databases into one integrated app

查看:385
本文介绍了我们可以将两个连接到不同数据库的Django / python应用程序组合成一个集成的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个独立的django应用程序连接到不同的dbs。我想制作一个父应用程序,将请求路由到两个子应用程序之一。



可以这样做吗?父应用程序不必连接到任何数据库,它应该只是将请求路由到子应用程序。



感谢您的帮助。

解决方案

这是可能的。在Django认为应用程序作为可以拼凑和组合的库。所以让我们说你有以下几点:


  1. 应用A连接到数据库A

  2. 应用程序B连接到db B

你可以创建一个django项目。将这两个应用程序放在安装应用程序INSTALLED_APPS中。请查看django文档中的多个数据库部分。这将解释如何配置您的应用程序,以便它们自动路由到正确的数据库。最后,您不需要创建第三个父应用程序。然后编辑项目的urls.py并为每个应用程序定义路由。您还可以从应用程序B调用应用程序A,并根据需要将请求从一个重定向到另一个。



示例urls.py 导入admin
管理员的p>

  autodiscover()
urlpatterns = patterns('',
url(r'^ admin / doc /',include('django.contrib.admindocs.urls')),
url '^ admin /',include(admin.site.urls)),

上面的例子将两个不同的应用程序路由到两个不同的URL。这两个应用程序都是 django.contrib.admindocs 和与Django一起发送的 django.contrib.admin 。该示例来自 Django教程的第二部分


I have two standalone django apps that connect to different dbs. I want to make a parent app which routes the requests to one of the two child apps.

Is it possible and how can I achieve this? The parent app doesn't have to connect to any db, it should just route the requests to the child apps.

Thanks for the help.

解决方案

This is possible. In Django think of apps as libraries that you can piece together and combine. So lets say you have the following:

  1. App A connects to db A
  2. App B connects to db B

You could just create one django project. Put both apps in your install apps INSTALLED_APPS. Check out the section on Multiple databases in the django docs. It will explain how to configure your apps so that they will automatically route to the correct database. Finally you don't really need to create a third parent app. Then edit your project's urls.py and define a route for each app. You can also call app A from App B, and redirect requests from one to the other as you need.

example urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

The example above will route two separate apps to two different urls. Both these apps django.contrib.admindocs and django.contrib.admin ship with Django. The example is taken from the 2nd part of the Django tutorial.

这篇关于我们可以将两个连接到不同数据库的Django / python应用程序组合成一个集成的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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