我是Django的坚定支持者,就像曾经我支持Flask一样。
Flask的经历
我在了解Python开发网站的第一站,就是Flask。Flask有着非常多的有点,比如代码非常优雅、框架非常简洁、插件非常丰富等等。
但是,作为一个网站建设的小菜鸡,我并没有利用Flask搭建起一个完整的个人网站。究其原因,还是因为Flask过于灵活,所谓,都是路,反而不知道如何选择路。
作为Flask的曾经使用者,我总结了两个Flask的缺陷,仅供参考:
第一:Flask最新的版本相比于旧版本变化比较大,很多网络上的教程都已经不适合使用了。比如,我经常逛的B站上,关于Flask的教程视频都已经非常老旧,里面使用的很多插件,甚至都已经停止更新。
第二:Flask由于其精简的设计,很多插件都需要自己从网络上查找,费时费力先搁一边不谈,开源插件的质量参差不齐,而且存在许多兼容性问题。我依稀还记得当时有一个非常重要的自定义指令插件、邮箱插件等都已经停止了更新,找新的插件让人心力交瘁。
Django初遇
我在Flask上遇到的问题,使我不得不考虑换一个更加省心省力的框架,从而开始了解Django。
在刚开始使用Django的时候,我的内心和很多排斥Django的人一样,觉得Django系统臃肿,而代码人天然排斥臃肿。
实际上,Flask的运行速度不一定比Django高,我曾经读过一个人对Python各个框架效率的测试,虽然不一定很官方,但是Flask得出的结论是和官网公布的结论不一致的,而Django并非最差的那个,而是处于中游偏上的位置。
另外,Django独特的应用设计也和Flask完全不同,在Django中,可以存在多个应用,需要在Settings文件中进行设置才能正常使用。
而这个优秀的设计反而成了很多Pyer的劝退警告,因为无法在入门阶段完成正确的设置,劝退了无数代码人。而我自己也是经过了多次的放弃和重学才逐步入门,反而觉得这个设计非常优秀。
使用Django开发网站的过程中,我也总结了几条Django不得不说的优势:
第一:无与伦比的后端管理系统,天然适合CMS类管理系统的开发,比如博客系统。使用Django自带的模型系统,创建数据库实体之后,只需要简单的讲模型在Admin中注册,就会自动生成后端管理系统,可以立即使用后端的数据库更新能力。博客系统有两个面,一个是面向阅读者的,这里只用于展示内容;一个是面向管理者的,这里用于更新内容。而博客的后端管理非常简单,本质上就是增删改查,自带系统完全能够满足。
第二:优雅简单的ORM系统,对于不熟悉SQL的同志,对象关系映射可能是最后的稻草。Django自带的模型系统非常优秀,例如外键、多对多等关系的实现非常简单。我自认为在Flask中使用的SQLAlchemy不如Django自带的简洁明了。
如何选择框架
当前,前后端分离是网站开发的主流,这对于前端人来说,是降低了网站的开发门槛。例如使用Node.js、Next.js等框架,就能快速的使用JS一把撸。最重要的是,前端人能做出来美轮美奂的前端页面,令人叹为观止。
而对于后端人来说,限制网站开发的最大困难是CSS,自己很难对自己写出来的页面有所认同,只能使用成熟的前端框架进行美化。而为了开发一个简单的博客系统去专门学习Vue.js、React.js是十分不划算的,还不如放弃前后端分离,采用原始开发模式,Jquery、bootstrap、Python快速开撸。
性能也是个人站长考虑的重要因素,我曾经就是为了选择一个更快的框架而货比三家。
后来才恍然大悟,所谓的性能不行,只是针对于计算机而言,在多个不同的框架选手中比个高低。
实际上,对于简单的博客系统,随便使用一个系统,只要不是代码写的稀烂,性能上基本不会有问题,使用的是高并发与否基本上不影响阅读,甚至于后端使用的是Mysql还是Sqlite也无关紧要,毕竟大部分博客网站撑死了也就百十篇文章。
网页加载慢更多的是网络上的瓶颈,比如你上传了一个好几M的大图片,以及加载了比较大的库,又或者有一些引用的库非常慢,当然这些都有非常简单的解决方案。
如果真的有一天,因为作者这句话造成了你的服务器性能瓶颈,那你就了不得了。
结论
好了,啰嗦了这么多,无非想说明两个事情:
第一:Django很行,开发效率很高,性能也不差,不要有心理上的顾虑;
第二:无非就是个博客,又不是淘宝双十一,市面上随便拉出来一个框架都能随便搞。
有投鼠忌器这点时间,使用Django都已经开发好网站上线了。
评论0
暂时没有评论