Django - 在不同的uwsgi实例上分离应用程序 - 在它们之间共享代码

时间:2023-01-24 13:15:03

I have created a ReST API using django-rest-framework which my mobile application interacts with.

我使用django-rest-framework创建了一个ReST API,我的移动应用程序与之交互。

I would now like to create a front end web application which will NOT utilize the API (which uses class-based views and model serializers ), but instead take advantage of django templates to serve the content.

我现在想要创建一个不使用API​​(使用基于类的视图和模型序列化器)的前端Web应用程序,而是利用django模板来提供内容。

I am using nginx with uwsgi to run and serve my apps to the user.

我正在使用带有uwsgi的nginx来运行并向用户提供我的应用程序。

To minimize the load traffic on the server I would like the API and web-app to run on separate uwsgi processes. I also would like to have different base URLs for the respective applications (e.g api.projectdomain.com / web.projectdomain.com)

为了最大限度地减少服务器上的负载流量,我希望API和Web应用程序在单独的uwsgi进程上运行。我还想为各自的应用程序提供不同的基本URL(例如api.projectdomain.com / web.projectdomain.com)

However, I also would like to have both the api and web-app in the SAME django project in order for them both to access the same model classes and admin tools.

但是,我也想在SAME django项目中同时拥有api和web-app,以便他们同时访问相同的模型类和管理工具。

Is this something that can be achieved and if so, where do I begin?

这是可以实现的吗?如果可以的话,我从哪里开始?

If not, how else would you recommend me to do in order to achieve the code sharing/URL distinguishing that I need?

如果没有,为了实现我需要的代码共享/ URL区分,您还有什么建议我这样做的?

Thanks!

谢谢!

1 个解决方案

#1


1  

First things first: having django REST in your application doesn't obligate you to use it's serializers and views, you can mix it with normal django views and REST api views.

首先要做的事情是:在你的应用程序中使用django REST不会强迫你使用它的序列化程序和视图,你可以将它与普通的django视图和REST api视图混合使用。

And yes, you can serve 2 separate web applications (that is not django apps) from one project. When you create new project using default template, django will create for you one serving (that is how I call them) app, with name of your project. You can create another one inside same project, add same apps in settings, configure it to use same database as main one, use different urlpatterns (and views) and serve it on another uwsgi process (or group of processes). There is only issue with having only one manage.py, but if both of your serving apps are using same database and same models, only urls and views differs, you can manage both apps at once, using only one manage.py for them.

是的,您可以从一个项目中提供2个独立的Web应用程序(不是django应用程序)。当您使用默认模板创建新项目时,django将为您创建一个服务(我称之为他们)应用程序,其中包含项目名称。您可以在同一个项目中创建另一个,在设置中添加相同的应用程序,将其配置为使用与主数据库相同的数据库,使用不同的urlpatterns(和视图)并在另一个uwsgi进程(或进程组)上提供。只有一个manage.py存在问题,但如果您的两个服务应用程序使用相同的数据库和相同的模型,只有网址和视图不同,您可以同时管理这两个应用程序,只使用一个manage.py。

Or you can just create separate settings and wsgi files, but in one serving app folder. It's totally up to you how you will arrange it.

或者您可以在一个服务应用程序文件夹中创建单独的设置和wsgi文件。这完全取决于你如何安排它。

#1


1  

First things first: having django REST in your application doesn't obligate you to use it's serializers and views, you can mix it with normal django views and REST api views.

首先要做的事情是:在你的应用程序中使用django REST不会强迫你使用它的序列化程序和视图,你可以将它与普通的django视图和REST api视图混合使用。

And yes, you can serve 2 separate web applications (that is not django apps) from one project. When you create new project using default template, django will create for you one serving (that is how I call them) app, with name of your project. You can create another one inside same project, add same apps in settings, configure it to use same database as main one, use different urlpatterns (and views) and serve it on another uwsgi process (or group of processes). There is only issue with having only one manage.py, but if both of your serving apps are using same database and same models, only urls and views differs, you can manage both apps at once, using only one manage.py for them.

是的,您可以从一个项目中提供2个独立的Web应用程序(不是django应用程序)。当您使用默认模板创建新项目时,django将为您创建一个服务(我称之为他们)应用程序,其中包含项目名称。您可以在同一个项目中创建另一个,在设置中添加相同的应用程序,将其配置为使用与主数据库相同的数据库,使用不同的urlpatterns(和视图)并在另一个uwsgi进程(或进程组)上提供。只有一个manage.py存在问题,但如果您的两个服务应用程序使用相同的数据库和相同的模型,只有网址和视图不同,您可以同时管理这两个应用程序,只使用一个manage.py。

Or you can just create separate settings and wsgi files, but in one serving app folder. It's totally up to you how you will arrange it.

或者您可以在一个服务应用程序文件夹中创建单独的设置和wsgi文件。这完全取决于你如何安排它。