Web上的Python:为什么像Django这样的框架很热门

tech2023-02-22  96

Created in the early 1990s by Guido van Rossum, Python has grown in popularity over the years. In 2016, Python is the 4th most popular language after Java, C and C++. Python is a general purpose programming language, and it can be used in a variety of fields. To quote Kenneth Love from Treehouse —

Python由Guido van Rossum于1990年代初创建,多年来逐渐流行起来。 在2016年,Python是仅次于Java,C和C ++的第四大流行语言 。 Python是一种通用的编程语言,可以在许多领域中使用。 引用《树屋》中的肯尼斯·洛夫(Kenneth Love) —

When I need to build a web app, I reach for Python. When I need to automate some small task on my system, I reach for Python. When I want to find the most common colors in an image, I reach for Python.

当我需要构建Web应用程序时,可以接触Python。 当我需要在系统上自动化一些小任务时,可以使用Python。 当我想在图像中找到最常见的颜色时,可以使用Python。

Python is a popular choice for writing scripts for testing and monitoring. Python has also been used for game development, and its ability to be integrated with other languages makes it very valuable in the process. Such is the popularity of Python that it’s also been used by George Lucas’ Industrial Light and Magic (responsible for special effects in the original Star Wars trilogy) to manage its complex production process.

Python是编写测试和监视脚本的流行选择。 Python也已用于游戏开发 ,它与其他语言集成的能力使其在此过程中非常有价值。 Python之所以如此受欢迎,是因为乔治·卢卡斯(George Lucas)的《工业轻与魔术》 ( Industrial Light and Magic) (负责原始《星球大战》三部曲中的特殊效果)也使用它来管理其复杂的生产过程。

Unlike PHP, it wasn’t built for the web in mind, and there are no core web functionalities that are integrated into Python. Hence, we must use a web framework to develop web applications in Python. Web developers have started using it for the web since the rise of popular frameworks like Django.

与PHP不同,它不是为Web构建的,并且没有集成到Python中的核心Web功能。 因此,我们必须使用Web框架以Python开发Web应用程序。 自Django之类的流行框架问世以来,Web开发人员已开始将其用于Web。

What makes Python the go-to language for an increasing number of developers when it comes to web development these days? We’ll try to find an answer to this question in this post.

如今,什么使Python 成为越来越多的开发人员使用的Web开发语言? 我们将在这篇文章中尝试找到该问题的答案。

为什么要使用Python? (Why use Python?)

适用于初学者的Python (Python for beginners)

The primary reason for the popularity of Python is the elegance of the code — the brevity and readability in particular. For instance, let us look at how Python and Java stack up against each other in terms of reversing a number:

Python普及的主要原因是代码的优雅-尤其是简洁和可读性。 例如,让我们看一下Python和Java在反转数字方面是如何相互对立的:

Programs to reverse a number in Java (left) and Python 2.7.x (right)

程序,用于反转Java(左)和Python 2.7.x(右)中的数字

Python provides a short learning curve, making it ideal for beginners to learn. In addition to that, if your project is Python-based and new developers aren’t familiar with it, the transition is easier.

Python提供了短的学习曲线,使其非常适合初学者学习。 除此之外,如果您的项目是基于Python的,而新开发人员对此并不熟悉,则过渡会更容易。

As Quora co-founder Adam D’Angelo says on the choice of Python for Quora’s development —

正如Quora的共同创始人Adam D'Angelo所说 ,在为Quora的开发选择Python时,

So far, we’ve been pretty happy with the choice … all of the early employees who’d been working with other languages in the past were happy to transition to Python, especially those coming from PHP.

到目前为止,我们对选择感到非常满意……过去曾经使用其他语言的所有早期员工都很乐意过渡到Python,尤其是那些来自PHP的人。

In fact, the webcomic xkcd came up with a cartoon on how easy it is to get things done in Python!

实际上,Webcomic xkcd提出了一个动画片,说明用Python完成任务有多么容易!

Further, Python has easy to use debugging tools. Although there are several debuggers and IDE tools, the default one is pdb, an interactive debugging tool which allows a developer to stop the execution of a program midway and assess the environment to better understand run time errors.

此外,Python具有易于使用的调试工具。 尽管有多种调试器和IDE工具,但默认的是pdb ,它是一种交互式调试工具,它使开发人员可以在中途停止程序的执行并评估环境,以更好地了解运行时错误。

远程服务器上的Python (Python on a remote server)

The management of packages (or modules as they are called) in Python is very easy too. Use a package installer like pip or easy_install and it can be used to install and remove packages.

Python中的软件包(或称为模块)的管理也非常容易。 使用pip或easy_install之类的软件包安装程序,可以用于安装和删除软件包。

Python is very portable too! The ease of transferring your development environment to a remote machine is commendable. You just need to export the packages, and install it in a virtual environment on the remote machine with just two commands.

Python也非常可移植! 值得称赞的是,可以轻松地将开发环境转移到远程计算机。 您只需要导出软件包,然后仅需两个命令即可将其安装在远程计算机上的虚拟环境中。

One more reason why Python is a good choice for web applications is the ability of running scripts which are not embedded to the web server (unlike running a PHP script.) Scripts are run as separate processes.

Python之所以成为Web应用程序的理想选择的另一个原因是能够运行未嵌入到Web服务器的脚本(与运行PHP脚本不同)。脚本是作为单独的进程运行的。

使用Python Web Framework进行Web开发 (Web Development using Python Web Frameworks)

Python’s web capabilities are not built into its core features, and therefore, we need the help of modules to enable web development in Python. If you want to develop very basic applications — such as a page for checking whether a server is running — you may use the cgi module.

Python的Web功能并未内置于其核心功能中,因此,我们需要模块的帮助才能启用Python中的Web开发。 如果要开发非常基本的应用程序(例如用于检查服务器是否正在运行的页面),则可以使用cgi模块 。

With an increase in the requirements, one needs to move to a Python framework like Django or Flask to be able to develop complex web based applications.

随着需求的增加,人们需要转向像Django或Flask这样的Python框架,以便能够开发复杂的基于Web的应用程序。

Most of these frameworks need to be coupled with a web server like Apache for production purposes, and this is where a minor bottleneck is present. As compared to PHP, ease of deployment is a bit difficult on a remote web server if you are doing it for the first time in your life (although we have covered that as well). However, the time that you invest would definitely reap rewards later.

为了生产目的,这些框架中的大多数都需要与Web服务器(如Apache)结合使用,这是一个小瓶颈。 与PHP相比,如果您是一生中第一次这样做,则在远程Web服务器上的易部署性会有些困难(尽管我们也对此进行了介绍 )。 但是,您投入的时间肯定会在以后获得回报。

Popular services like Instagram, Reddit, Quora and Disqus were all developed using Python.

Instagram , Reddit , Quora和Disqus等流行服务都是使用Python开发的。

Django-最受欢迎的Python Web框架 (Django — the most popular Python Web Framework)

A question naturally arises: Why use Python for web development? My answer is firstly to stop thinking of it as just being about using just “Python” for web development. It’s frameworks like Django and Flask that make web development in Python so feature rich and powerful. For instance, the following are a few things that Django does really well as compared to any other web framework:

自然会产生一个问题:为什么要使用Python进行Web开发? 我的回答是首先停止将其视为仅使用“ Python”进行Web开发。 像Django和Flask这样的框架使Python的Web开发变得功能强大。 例如,以下是Django与其他任何Web框架相比确实能做得很好的事情:

Easy Learning Curve. Just like Python, Django is easy to learn. You just need to go through this step-by-step tutorial on Django’s website, which does a great job of explaining the basic features of Django clearly. On the other hand, learning something like Ruby on Rails might take a longer time.

易学曲线 。 就像Python一样,Django很容易学习。 您只需要阅读 Django网站上的分步教程 ,就可以很好地解释Django的基本功能。 另一方面,学习Ruby on Rails之类的内容可能需要更长的时间。

Creating SQL tables automatically. Django does this for you once you’ve specified the structure. (Note: SQLAlchemy does the same, and there’s a heated debate about this on Hacker News, if you’re interested.)

自动创建SQL表 。 指定结构后,Django会为您完成此操作。 (注意:SQLAlchemy也是一样,如果您对此感兴趣,请在Hacker News上进行激烈的辩论 。)

Generate Forms. Once you’ve created a Form class in Django and linked it to a model, the form generator in Django takes care of rendering forms, data validation and saving the data.

生成表格 。 在Django中创建Form类并将其链接到模型后,Django中的表单生成器将负责呈现表单,验证数据并保存数据。

Admin Interface. Just like the SQL tables, once you’ve decided the structure, Django creates an admin interface that allows you to manage the database. (It’s just like PhpMyAdmin built into Django.)

管理界面 。 就像SQL表一样,一旦您确定了结构,Django就会创建一个管理界面,使您可以管理数据库。 (就像Django内置的PhpMyAdmin一样。)

Django Shell. The Python shell, within the Django project’s environment, is what the Django shell provides. This feature is useful in debugging, which is relatively more difficult in PHP.

Django Shell 。 在Django项目环境中的Python外壳就是Django外壳所提供的。 此功能在调试中很有用,在PHP中这相对较难。

If these are not enough, there’s cutting edge development going on in Python. Two exciting services in Python are Tornado (for asynchronous tasks) and Celery (for job scheduling). You may want to look at them if they fulfill your needs.

如果这些还不够的话,Python将会进行最前沿的开发。 Python中两个激动人心的服务是Tornado (用于异步任务)和Celery (用于作业调度)。 如果它们满足您的需求,则可能需要查看它们。

Django虚拟主机 (Django Web Hosting)

The biggest hurdle for someone to start working on Django is the availability of web hosts. Obviously, due to the popularity of PHP, the number of hosts are high and hosting is relatively cheap (but not necessarily value for money, as cheap hosting is often shared). However, there are hosts like AWS and Heroku that allow you to host your Django applications for free (within certain limits). For shared hosts at a pocket-friendly price, you may go for a host like WebFaction.

有人开始使用Django的最大障碍是网络主机的可用性。 显然,由于PHP的普及,主机的数量很高并且托管相对便宜(但不一定物有所值,因为便宜的托管经常被共享)。 但是,有些主机(例如AWS和Heroku)可让您免费 (在一定限制内) 托管Django应用程序 。 对于价格合理的共享主机,您可以选择WebFaction之类的主机。

The most popular cloud hosting services today like AWS, Digital Ocean and Microsoft Azure provide root access to the server, which allows you to manually set up your environment. Digital Ocean, in fact, has come up with one-click Django deployment. Django hosting might not be as popular as the PHP counterparts, but reliable options at different pricing levels are available.

当今最流行的云托管服务(如AWS , Digital Ocean和Microsoft Azure)提供对服务器的根访问,这使您可以手动设置环境。 实际上,Digital Ocean提出了一键式Django部署 。 Django托管可能不像PHP托管那样流行,但是可以使用不同定价级别的可靠选项 。

最后的想法 (Final Thoughts)

The purpose of this article is not to tell you that Python is the solution to every problem! It is to highlight the parameters of Python and its web frameworks, which make life easier for web developers.

本文的目的不是要告诉您Python是解决所有问题的方法! 这是为了突出Python及其Web框架的参数,这使Web开发人员的工作更加轻松。

Have you tried Python and its web frameworks before? Do let us know in the comments below.

您之前是否尝试过Python及其Web框架? 请在下面的评论中告诉我们。

翻译自: https://www.sitepoint.com/python-on-the-web/

最新回复(0)