为什么烧瓶可能比Django更好的选择

As we all know or might have heard from others (only if you are into web development) that there are two very popular web frameworks. One is, of course, the most famous Django web framework, and the other which is I would say comes just below Django in the fame chart of web frameworks i.e. Flask. There are of course others in the market but for now, I’ll just talk about these two. I shall discuss some of the points that you should remember or consider while choosing either Django or Flask.

大家都知道,也可能从其他人(前提是你到听说过Web开发 )有两个非常流行的Web框架 。 当然,一个是最著名的Django Web框架 ,而我想说的另一个是Web框架(即Flask)的人气排行榜中的Django以下。 市场上当然还有其他人,但是现在,我只讨论这两个。 我将讨论选择Django或Flask时应记住或考虑的一些要点。

框架到底是什么? (What exactly is a framework?)

Those of you, who are into programming, you build programs that require a compiler or an interpreter to convert the source code into byte code, and thus you make command-line applications. The user will then run your script (Python) via a shell and may or may not pass arguments and/or pass the input through stdin (standard input). Now, this is a tedious job for a user because the user has to again install the proper interpreter or compiler to run such programs. Years ago, people felt the need for building standalone applications which do not require the user to install additional programs or compilers, and thus were called Desktop applications. Today with the advent of widespread internet connectivity and a handful of Operating Systems to choose from (both desktop and mobile) it has become a common practice to build web applications rather than OS-based applications. The main advantage of web apps is that users do not need to install apps on their devices to use them. They can directly access the application from their web browser.

那些从事编程工作的人会构建需要编译器或解释器才能将源代码转换为字节码的程序,从而创建命令行应用程序。 然后,用户将通过外壳运行您的脚本( Python ) ,并且可以传递或不传递参数和/或通过stdin (标准输入)传递输入。 现在,这对于用户而言是一项繁琐的工作,因为用户必须再次安装适当的解释器编译器才能运行此类程序。 几年前,人们感到需要构建独立的应用程序 ,这些应用程序不需要用户安装其他程序或编译器,因此被称为“ 桌面应用程序” 。 如今,随着广泛的Internet连接和少数操作系统(台式机和移动机)可供选择的出现,构建Web应用程序已成为一种普遍做法。 而不是基于OS的应用程序 。 Web应用程序的主要优点是用户无需在设备上安装应用程序即可使用它们。 他们可以从Web浏览器直接访问该应用程序。

If you wish to build a web application then you would want to use Python to develop your application and use a so-called web framework. There are lots of tasks in building a web application. The two main tasks are to build the backend and the frontend along with a proper user interface. Then comes attaching everything together and hosting on a web server, so that users can easily navigate to your application in their browser. Here comes the functionality of a web framework. A web framework basically implements all the functionalities of a web application, like mapping URLs to certain blocks of python codes, so that when a user routes to a particular link the corresponding job is done which linked to that application route. We’ll see more of this so-called ‘routing’ later in this article.

如果要构建Web应用程序,则需要使用Python来开发应用程序并使用所谓的Web框架。 构建Web应用程序有很多任务。 两个主要任务是构建后端前端以及正确的用户界面。 然后将所有内容附加在一起并托管Web服务器上 ,以便用户可以在其浏览器中轻松导航到您的应用程序。 这是Web框架的功能。 Web框架基本上实现了Web应用程序的所有功能,例如将URL映射到python代码的某些块,以便当用户路由到特定链接时,完成了与该应用程序路由链接的相应作业。 在本文后面的部分中,我们将更多地介绍这种所谓的“ 路由 ”。

Flask和Django实现之间的区别 (Difference between Flask and Django implementation)

Flask implements the minimum required things for a web application to actually work. All the frontend and backend implementation needs to be done by the user. But this is not the disadvantage, rather it’s best if most of the work is left for the developer because every application has its own kind of implementation and every application is different from the other. Again, as a developer, if your application is too large because of unwanted files and codes, then definitely it is not a good implementation. Therefore, Flask lets you create just what you need for your web application.

Flask实现了Web应用程序实际运行所需的最低要求。 所有前端和后端实现都需要由用户完成。 但这不是缺点,而是最好将大部分工作留给 开发人员,因为每个应用程序都有自己的实现类型,并且每个应用程序都彼此不同。 再次,作为开发人员,如果您的应用程序由于不需要的文件和代码而变得太大,那么绝对不是一个好的实现。 因此,Flask允许您创建Web应用程序所需的内容。

On the other hand, Django gives a lot more than just the bare minimum. It creates a project structure and creates files that are of course needed but with a lot of unwanted code and data. You might be thinking that ready-to-use files will make your life easier. No, you will have to edit those files according to your needs and requirements and a change in the settings will affect your application both the good way and the bad way. Application created with the Django framework will definitely provide you with a lot of more extra features in comparison to Flask, but these features come at a cost. Your project size increases and if you decide to keep those features running in your production too, then you will definitely have a large application size which would in turn cost you extra hosting space.

另一方面,Django所提供的不只是最低要求。 它创建了一个项目结构并创建了当然需要的文件,但其中包含许多不需要的代码和数据。 您可能会认为随时可用的文件将使您的生活更轻松。 不,您将必须根据需要和要求来编辑这些文件,并且设置的更改会影响您的应用程序的好坏。 与Flask相比,使用Django框架创建的应用程序肯定会为您提供许多其他功能 ,但是这些功能是有代价的。 您的项目规模会增加,如果您决定让这些功能也继续在生产环境中运行,那么您肯定会拥有较大的应用程序规模,这反过来又会花费您额外的托管空间。

您如何对比Flask和Django? (How would you contrast Flask and Django?)

烧瓶 (Flask)

  • Flask is a micro and lightweight web framework

    Flask是一个微型轻量级的Web框架
  • Flask framework provides you simplicity, flexibility, and fine-grained control

    Flask框架为您提供了简单性,灵活性和细粒度的控制
  • It is unopinionated i.e., it lets you as a developer decide what and how you want to implement the various things

    它是不受质疑的,也就是说,它使您作为开发人员可以决定要实现什么以及如何实现各种功能
  • It can be used to build comparatively simpler web applications

    它可以用来构建相对简单的Web应用程序

Django的 (Django)

  • Django is a full-stack web framework

    Django是一个全栈式Web框架
  • Django provides all-inclusive experience during your development process

    Django在您的开发过程中提供了全方位的经验
  • On project creation, you will get an admin panel, database interfaces, an Object Relational Model (ORM), and most importantly you will get a pre-defined project/directory structure

    在创建项目时,您将获得管理面板,数据库界面, 对象关系模型(ORM) ,最重要的是,您将获得预定义的项目/目录结构

  • It can be used to build large and complex web applications

    它可用于构建大型复杂的Web应用程序

我如何谈论现实生活场景? (How about I talk about the real-life scenario?)

Both Flask and Django are used to build web-based applications using their respective frameworks. Both are used at a large scale for several high-traffic websites.

FlaskDjango均使用各自的框架来构建基于Web的应用程序。 两者都被大量用于一些高流量的网站。

The following Quora link tells you about some of the websites that use Flask. You will be definitely surprised because I was too!

以下Quora链接介绍了一些使用Flask的网站。 您肯定会感到惊讶,因为我也是!

Below is a link clicking which you’ll be redirected to a website where they have shown a list of all the major and famous web apps that use the Django framework:

以下是单击链接的链接,您将被重定向到一个网站,该网站显示了使用Django框架的所有主要和著名的Web应用程序的列表:

以下文章的一些统计信息: (Some statistics from the following article:)

This statistic has been taken from iflexion.com. This contains further information too. Here is the link: https://www.iflexion.com/blog/python-flask-vs-django

该统计数据来自iflexion.com。 这也包含更多信息。 这是链接: https : //www.iflexion.com/blog/python-flask-vs-django

Image for post
Source: 消息来源https://www.iflexion.com/blog/python-flask-vs-django https ://www.iflexion.com/blog/python-flask-vs-django

Flask和Django的技术比较 (A technical comparison of both Flask and Django)

模板引擎 (Template Engine)

Flask is developed based on Jinja2 template engine, which for your information is actually inspired by Django’s template system. It enables developers to write web templates in an expressive language thereby speeding up the application development process. As you have probably guessed, on the other hand, Django has a built-in template engine, which actually lets developers spent less time and effort on the template building part of the process. Django provides something called Django Template Language (DTL) which is used for creating custom templates for your application.

Flask是基于Jinja2开发的 模板引擎,该信息实际上是受Django模板系统启发的。 它使开发人员可以用一种表达性语言编写Web模板,从而加快了应用程序开发过程。 另一方面,您可能已经猜到了,Django有一个内置的模板引擎,它实际上使开发人员可以在模板构建过程中花费更少的时间和精力。 Django提供了一种称为Django模板语言(DTL)的东西 ,用于为您的应用程序创建自定义模板。

项目布局 (Project Layout)

In Flask, developers are required to create separate projects for building separate applications. But the developers are provided with the option of adding multiple models and views to the very same application they are working on. But Django makes developers divide a single project into multiple applications. Then, developers can write individual applications, and add functionality to their main web application by adding the small applications into the project.

在Flask中,要求开发人员创建单独的项目以构建单独的应用程序。 但是,开发人员可以选择将多个模型和视图添加到他们正在处理的同一应用程序中。 但是Django使开发人员将单个项目分为多个应用程序。 然后,开发人员可以编写单个应用程序,并通过将小型应用程序添加到项目中来向其主要Web应用程序添加功能。

数据库支持 (Database Support)

Django developers generally use the ORM system. In order to implement databases like Oracle, SQLite, MySQL, and PostgreSQL, ORMs are a real necessity. The ORM system helps developers avoid writing lengthy SQL queries. This built-in ORM system unlike Django is not provided by Flask. It would require libraries like SQLAlchemy to use database functionalities in Flask. The developers can perform common database actions like, SELECT, INSERT, UPDATE, and DELETE by writing and executing normal SQL queries.

Django开发人员通常使用ORM系统。 为了实现OracleSQLiteMySQLPostgreSQL之类的数据库,ORM是真正的必需品。 ORM系统可帮助开发人员避免编写冗长的SQL查询 。 Flask不提供与Django不同的内置ORM系统。 这就要求像SQLAlchemy这样的库才能在Flask中使用数据库功能。 开发人员可以通过编写和执行普通SQL查询来执行常见的数据库操作,例如SELECT,INSERT,UPDATE和DELETE

灵活性 (Flexibility)

The advanced features that come with Django allow the users to build a variety of web applications without using any other third-party tools and libraries. But developers lack the very needed option to make changes to the in-built modules provided by Django which is a major drawback. The modules are, as you can see are non-customizable. Therefore, the developers have to use built-in features provided by the Django web framework to create web applications. On the other hand, Flask, being a micro but extensible web framework, lets developers create web applications in a more flexible fashion by using web development tools and libraries that are open-source and can be added or removed as when required. Trust me, when I started web development, I found Flask much easier and comfortable, and I still use Flask for small projects.

Django随附的高级功能使用户无需使用任何其他第三方工具和库即可构建各种Web应用程序。 但是开发人员缺少用于更改Django提供的内置模块的非常必要的选项,这是一个主要缺点。 如您所见,这些模块是不可定制的。 因此,开发人员必须使用Django Web框架提供的内置功能来创建Web应用程序。 另一方面,Flask是一个微型但可扩展的Web框架,它使开发人员可以使用开放源代码的Web开发工具和库以更灵活的方式创建Web应用程序,并且可以在需要时添加或删除它们。 相信我,当我开始Web开发时,我发现Flask更加轻松和舒适,并且我仍然在小项目中使用Flask。

那么我们是否应该继续进行动手体验呢? (Shall we then move onto getting a hands-on experience?)

首先让我们在Django中进行编码! (所有命令都可以在shell或cmd中执行) (Let’s code in Django first! (All commands can be executed in shell or cmd))

1. Install Django by running the following command:

1.通过运行以下命令安装Django

pip install Django==3.1.1

2. After a successful installation, we need to create a project. Run the following:

2.成功安装后,我们需要创建一个项目。 运行以下命令:

django-admin startproject newproject

This creates a new project named newproject using the django-admin command which was installed during Django installation. After successful creation your directory structure would look something like this:

这将使用在Django安装过程中安装的django-admin命令创建一个名为newproject的新项目。 创建成功后,您的目录结构将如下所示:

What exactly your directory structure will look like
Project directory structure
项目目录结构

3. Next, we need to use Django to create an app. Please note one project can contain multiple apps. From the outer, newproject directory run the following:

3.接下来,我们需要使用Django创建一个应用程序。 请注意,一个项目可以包含多个应用程序。 从外部的newproject目录运行以下命令:

python manage.py startapp helloworld

This creates the helloworld app and makes it part of newproject

这将创建helloworld应用并将其纳入newproject

4. Next, we shall configure the URL routing. Open the created helloworld/views.py and add the following:

4.接下来,我们将配置URL路由。 打开创建的helloworld / views.py并添加以下内容:

from django.http import HttpResponse


def index(request):
  return HttpResponse("Hello, World!")

5. Now we shall create a urls.py file for this application. We shall create helloworld/urls.py and add the following:

5.现在,我们将为此应用程序创建一个urls.py文件。 我们将创建helloworld / urls.py并添加以下内容:

from django.conf.urls import url
from . import views


urlpatterns = [
  url(r'^$', views.index, name='index'),
]

6. The above is the URL configuration for our helloworld application. We will be needing a URL configuration for our project too. We shall edit the automatically created file newproject/newproject/urls.py. This one belongs to the entire project the previous one belongs to the app alone. Add the following in this file:

6.以上是我们的helloworld应用程序的URL配置。 我们也将需要为我们的项目配置URL。 我们将编辑自动创建的文件newproject / newproject / urls.py。 此项目属于整个项目,而上一个项目仅属于应用程序。 在此文件中添加以下内容:

from django.conf.urls import include, url


urlpatterns = [ 
  url(r'^hello/', include('helloworld.urls')), 
]

7. Now go back to the outer directory /newproject that contains manage.py and run the following:

7.现在回到包含manage.py的外部目录/ newproject并运行以下命令:

python manage.py runserver

This will run the Django development server at localhost. As you can see from the terminal output our server is running at http://127.0.0.1:8000/ (Note that it is http and not https)

这将在localhost运行Django开发服务器。 从终端输出中可以看到,我们的服务器运行在http://127.0.0.1:8000/ (请注意,它是http而不是https )

Your server after it starts might look like this
Successful server run
服务器成功运行

We can now visit http://127.0.0.1:8000/hello to view our “Hello, World!”. (The /hello is important because this will be a GET /hello request to the server)

现在,我们可以访问http://127.0.0.1:8000/hello来查看“世界,您好!” 。 ( / hello很重要,因为这将是对服务器的GET / hello请求)

Your browser output is going to look like this
Browser output
浏览器输出

现在,让我们看看在Flask中完成整个工作有多容易! (所有命令都可以在shell或cmd中执行) (Now let’s take a look at how easier it is to do this whole job in Flask! (All commands can be executed in shell or cmd))

1. Install Flask by running the following command:

1.通过运行以下命令安装Flask

pip install Flask== 1.1.2

2. After successful installation, create a python file called helloworld.py and insert the following:

2.成功安装后,创建一个名为helloworld.py的python文件并插入以下内容:

from flask import Flask


app = Flask(__name__)


@app.route("/")
def hello():
  return "Hello, World!"
  
if __name__ == "__main__":
  app.run()

This does almost all of the work first, it creates an app called app then it configures a URL route “/” which is actually index i.e. when the browser sends an http request at the website IP or the resource name (Example: example.com), there will be a GET / request sent to the server. Therefore, from our app above it will return a message “Hello, World!” n text/html document type. This is also known as the default root of the application. Finally the last line we see that on running the python script, it would trigger main and thus, app.run() will be executed which in turn will start the application.

这几乎完成了所有工作,首先创建了一个名为app的应用程序,然后配置了URL路径“ /” ,该路径实际上是索引,即,当浏览器通过网站IP或资源名称发送http请求时(例如:example.com) ),将有GET /请求发送到服务器。 因此,从我们上面的应用程序中,它将返回一条消息“ Hello,World!”。 n 文本/ html文档类型。 这也称为应用程序的默认根目录 。 最后,在最后一行中,我们看到在运行python脚本时,它将触发main,因此将执行app.run() ,从而启动应用程序。

3. Run the following command to start the web application to look at the results:

3.运行以下命令以启动Web应用程序以查看结果:

python helloworld.py

This will run the Flask server at localhost. As you can see from the terminal output our server is running at http://127.0.0.1:5000/ (Note that it is http and not https)

这将在localhost运行Flask服务器。 从终端输出中可以看到,我们的服务器运行在http://127.0.0.1:5000/ (请注意,它是http而不是https )

Your server after it starts might look like this
Successful server run
服务器成功运行

We can now visit http://127.0.0.1:8000/ to view our “Hello, World!”.

现在,我们可以访问http://127.0.0.1:8000/以查看“世界您好!”

Your browser output is going to look like this
Browser output
浏览器输出

As you can it was barely a 3-step job to get a server running on your localhost and display “Hello, World!” in the case of Flask implementation whereas it took around 7 good steps to get a Django server running to do the same job. Please note that by default, Flask runs on port 5000, and Django runs on port 8000.

尽您所能,让服务器在本地主机上运行并显示“ Hello,World!” 仅需 三步 。 在Flask实施的情况下,花了大约7个好的步骤才能使Django服务器运行以完成相同的工作。 请注意,默认情况下, Flask在端口5000上运行,而Django在端口8000上运行。

Now comes the real question which one should you choose? Flask or Django? Both have their own perks and limitations. Choose Flask if you are focused on having more control about what components to use and what not (like databases, etc.) And, choose Django, if you are in need or want to develop basic straightforward applications such as blogs, e-stores, etc. where you would want to use all the standard things in the most obvious ways.

现在是真正的问题,您应该选择哪一个? 烧瓶还是Django? 两者都有自己的特权和局限性。 如果您专注于对要使用的组件和不使用的组件(例如数据库等)有更多控制,请选择Flask。如果您需要或想要开发基本的简单应用程序(例如博客,电子商店,等您想以最明显的方式使用所有标准内容的地方。

结论 (Conclusion)

In this article, I tried to provide a basic introduction to the framework, Flask and Django frameworks, followed by a good comparison between the two, and then I tried to explain to you how to create a basic application on both these frameworks.

在本文中,我试图提供对框架Flask和Django框架的基本介绍,然后对两者进行很好的比较,然后尝试向您解释如何在这两个框架上创建基本应用程序。

In short, I think Django is a heavy framework if you are trying to learn about frameworks and do a little web programming by developing a small app and at times it could become very difficult for you if you do not have a basic understanding of web frameworks. But yes, on the other hand, the extra work that Django does for you will be very useful and can save a lot of time.

简而言之,如果您想学习框架并通过开发小型应用程序进行一些Web编程,那么我认为Django是一个繁重的框架,如果您对Web框架没有基本的了解,有时对您来说将变得非常困难。 但是,是的,另一方面,Django为您所做的额外工作将非常有用,并且可以节省大量时间。

Still, confused about which one to use? Use Flask to get a hang of the entire web application development process and you’ll not find stuff that is of no use to you in Flask. Django will make things complicated for you if you’re doing it for the first time.

仍然对使用哪个感到困惑? 使用Flask可以掌握整个Web应用程序开发过程的全部内容,并且在Flask中找不到对您无用的东西。 如果您是第一次使用Django,Django将使您的工作变得复杂。

翻译自: https://medium.com/@rajarsi3997/why-flask-could-be-a-better-choice-than-django-c3f018edb963

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值