Python Web 开发 - 有或没有框架

2022-01-16 00:00:00 python frameworks

问题描述

我计划将 PHP 应用程序移植到 Python.该应用程序主要是关于数据收集和处理.主应用程序作为独立的命令行应用程序运行.该应用程序有一个 Web 界面,基本上是一个非常轻量级的报告界面.

I am planning on porting a PHP application over to Python. The application is mostly about data collection and processing. The main application runs as a stand alone command line application. There is a web interface to the application which is basically a very light weight reporting interface.

我没有在 PHP 版本中使用框架,但作为 Python 新手,我想知道使用 Django 或至少 Genshi 之类的东西是否有利.需要注意的是,我不希望我的应用程序分发被我需要与应用程序一起分发的框架部分所淹没.

I did not use a framework in the PHP version, but being new to Python, I am wondering if it would be advantageous to use something like Django or at the very least Genshi. The caveat is I do not want my application distribution to be overwhelmed by the framework parts I would need to distribute with the application.

在这种情况下,仅使用 Python 中的 cgi 导入是最好的方法吗?我倾向于认为框架开销太大,但也许我并没有以非常python"的方式思考它们.在这种情况下您有什么建议?

Is using only the cgi import in Python the best way to go in this circumstance? I would tend to think a framework is too much overhead, but perhaps I'm not thinking in a very "python" way about them. What suggestions do you have in this scenario?


解决方案

命令行 Python,IMO,绝对是第一位的.让它发挥作用,因为这是您所做工作的核心.

The command-line Python, IMO, definitely comes first. Get that to work, since that's the core of what you're doing.

问题在于,从命令行应用程序中使用 Web 框架的 ORM 并不明显.Django 提供了从命令行应用程序使用 ORM 的具体说明.起初这些很烦人,但我认为从长远来看它们是救生员.我大量使用它来上传大量客户提供的文件.

The issue is that using a web framework's ORM from a command line application isn't obvious. Django provides specific instructions for using their ORM from a command-line app. Those are annoying at first, but I think they're a life-saver in the long run. I use it heavily for giant uploads of customer-supplied files.

不要使用纯 CGI.这不是不可能的,但是太多的事情可能会出错,而且它们都已经被框架解决了.为什么要重新发明一些东西?用别人的代码就好了.

Don't use bare CGI. It's not impossible, but too many things can go wrong, and they've all been solved by the frameworks. Why reinvent something? Just use someone else's code.

框架涉及学习,但没有真正的开销".他们并不慢.它们是您不必编写或调试的代码.

Frameworks involve learning, but no real "overhead". They're not slow. They're code you don't have to write or debug.

  1. 学习一些 Python.

  1. Learn some Python.

学习 Django 教程.

开始构建网络应用.

一个.启动一个 Django 项目.在该项目中构建一个小型应用程序.

a. Start a Django project. Build a small application in that project.

b.使用 Django ORM 构建新模型.为模型创建一个 Django 单元测试.确保它有效.您将能够使用默认的管理页面并进行大量操作.只是不要构建整个网站.

b. Build your new model using the Django ORM. Create a Django unit test for the model. Be sure that it works. You'll be able to use the default admin pages and do a lot of playing around. Just don't build the entire web site yet.

让您的命令行应用程序使用 Django ORM 工作.本质上,您必须对设置文件进行微调才能使该应用程序正常工作.请参阅 settings/configuration 部分.

Get your command-line app to work using Django ORM. Essentially, you have to finesse the settings file for this app to work nicely. See the settings/configuration section.

一旦你的命令行和默认管理员运行,你就可以完成网络应用程序.

Once you've got your command line and the default admin running, you can finish the web app.

这是框架的黄金法则:这是您不必编写、调试或维护的代码.使用它们.

Here's the golden rule of frameworks: It's code you don't have to write, debug or maintain. Use them.

相关文章