Python Web开发-有无框架 [英] Python web development - with or without a framework

查看:64
本文介绍了Python Web开发-有无框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将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 教程.

开始构建Web应用.

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

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

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

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正常工作.本质上,您必须精简设置文件才能使该应用程序正常运行.请参见设置/配置部分.

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.

这篇关于Python Web开发-有无框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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