When you learn a back-end framework, you become used to doing projects in a certain way. My first framework was Express (the E in MERN stack), a very minimalist JavaScript framework.
当您学习后端框架时,就会习惯于以某种方式进行项目。 我的第一个框架是Express(在MERN堆栈中的E),这是一个非常简单JavaScript框架。
I would describe Express as a blank canvas. The boilerplate code will get your development server up and running but not much else. You the developer will then install lots of third-party packages known as ‘middleware’ to get things running the way you like them. Express gives developers lots of freedom and flexibility, but starting a project from scratch is a lot of work.
我将Express形容为一块空白画布。 样板代码将使您的开发服务器启动并运行,但没有太多其他功能。 然后,您将为开发人员安装许多称为“中间件”的第三方程序包,以使您的程序以您喜欢的方式运行。 Express为开发人员提供了许多自由和灵活性,但是从头开始一个项目需要大量工作。
Django is a framework that uses Python instead of JavaScript. Its main difference from Express is there are a lot more features built into the framework, many of which can be customized to your own tastes.
Django是使用Python而不是JavaScript的框架。 它与Express的主要区别在于框架中内置了许多功能,其中许多功能都可以根据自己的喜好进行定制。
Moving from Express to Django has made me a more productive developer. Having lots of features built-in makes it quick and easy to get projects up and running, leaving me with more time to focus on the meat of an application.
从Express迁移到Django,使我变得更有生产力。 内置许多功能可以快速轻松地启动和运行项目,这使我有更多时间专注于应用程序。
Today, I’m sharing with you 6 good bits and 3 not so good bits about Django.
今天,我将与您分享6个关于Django的好地方和3个不是很好的地方。
优点: (The good bits:)
1. Django管理面板 (1. The Django Admin panel)
This is a convenient, visual way to explore and manage your data. For Node developers, it is similar to Atlas for MongoDB but built into your application.
这是浏览和管理数据的便捷,直观的方式。 对于Node开发人员,它类似于适用于MongoDB的Atlas,但内置于您的应用程序中。
The best part of the Admin panel is that your clients can use it as well. You, the developer, can set up user groups and permissions to control what they can and can’t do. You can even customize the Admin panel with the client’s branding.
“管理”面板中最好的部分是您的客户也可以使用它。 您(开发人员)可以设置用户组和权限来控制他们可以做什么和不能做什么。 您甚至可以使用客户的品牌自定义管理面板。
The Django admin panel provides a convenient and customizable way to manage your data. Image from: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Admin_site Django管理面板提供了一种方便且可自定义的方式来管理数据。 图片来自: https : //developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Admin_siteFor me personally, the Admin comes into its own for large enterprise-scale projects. When a project has upwards of 50 models, the Admin panel gives new developers a convenient way to explore the data and get their bearings.
就我个人而言,对于大型企业规模的项目,管理员应运而生。 当一个项目有50个以上的模型时,“管理”面板为新开发人员提供了一种方便的方法来探索数据并获取他们的方向。
2. REPL外壳 (2. The REPL shell)
REPL stands for Read Eval Print Loop. It’s basically a Python shell, a bit like the console in your browser’s development tools.
REPL代表读取评估打印循环。 它基本上是一个Python shell,有点像浏览器开发工具中的控制台。
A Python REPL shell is built into Django. This lets you import your models and start playing around with database queries. This is great for developers who are new to Python and Django as it is a place to experiment and get instant feedback.
Django内置了Python REPL Shell。 这使您可以导入模型并开始使用数据库查询。 这对于刚接触Python和Django的开发人员来说非常有用,因为它是进行实验并获得即时反馈的地方。
3.数据库 (3. Databases)
When you start a Django project, it comes already set up with a SQLite database.
当您启动Django项目时,该项目已经使用SQLite数据库进行了设置。
SQLite might not be a production-grade database, but it’s great for small or personal projects. Having a light-weight database already setup will help you get up and running quickly.
SQLite可能不是生产级数据库,但对于小型或个人项目而言非常有用。 已经设置了轻量级的数据库将帮助您快速启动并运行。
If you need something more powerful, setting up a different database like PostgreSQL is no harder than setting up Mongo DB for Express.
如果您需要更强大的功能,那么设置诸如PostgreSQL之类的其他数据库并不比设置Mongo DB for Express困难。
4.基于类的视图 (4. Class-based views)
Views contain the logic for each page. If you are used to the Model-View-Controller pattern, then think of a Django View as a Controller.
视图包含每个页面的逻辑。 如果您习惯了Model-View-Controller模式,那么可以将Django View视为Controller。
In Node, controllers are written as functions. They take a request and return a template or a 404 error.
在Node中,控制器被编写为函数。 他们接受请求并返回模板或404错误。
In Django, developers have the option of writing their views as a function or as a class. Classes can inherit from one of Django’s own base classes.
在Django中,开发人员可以选择将视图作为函数或类编写。 类可以从Django自己的基类之一继承。
There are classes for creating, updating, and deleting models. There are also classes for common authentication tasks like logging in, changing a password, and logging out.
有用于创建,更新和删除模型的类。 还有一些用于常见身份验证任务的类,例如登录,更改密码和注销。
What is great about these built-in classes is that they already contain the functionality for common tasks, so very little additional code is needed. If the built-in class isn’t entirely suitable for your needs, then you can rewrite any method inside your class.
这些内置类的优点在于它们已经包含了用于常见任务的功能,因此几乎不需要其他代码。 如果内置类不完全适合您的需求,那么您可以重写类中的任何方法。
The logic for signing up only required 4 lines of modifications after inheriting the built-in CreateView class. Form validation and writing to the database is already built-in. 继承内置的CreateView类后,用于签名的逻辑仅需要进行4行修改。 表单验证和写入数据库已经内置。They take some getting used to, particularly understanding what’s going on under the hood, but they are brilliant for speeding up development.
他们习惯了一些习惯,尤其是了解幕后的情况,但是他们对于加快开发速度非常聪明。
5.强大的查询集 (5. Powerful Querysets)
Django has a Queryset API built in to make querying the database easier. If you are using PostgreSQL, this means you don’t have to write your queries in SQL.
Django内置了Queryset API,可简化数据库查询。 如果使用的是PostgreSQL,则意味着您不必用SQL编写查询。
Instead, the syntax with dot notation will be very familiar to JavaScript developers.
取而代之的是,JavaScript开发人员将非常熟悉带点表示法的语法。
posts = Post.objects.order_by(‘-date’).filter(is_published=True)
It’s also really easy to use Querysets for searches. For example:
使用查询集进行搜索也非常容易。 例如:
posts = queryset_list.filter(body__icontains=keywords)
6.无痛表格 (6. Pain-free forms)
For forms, Django passes the form object into the HTML template. This means you don’t have to create tags for every single field, nor do you have to create placeholders for error messages. In React, you have to bind the user input to a state, which isn’t necessary for Django.
对于表单,Django将表单对象传递到HTML模板中。 这意味着您不必为每个字段都创建标记,也不必为错误消息创建占位符。 在React中,您必须将用户输入绑定到状态,这对于Django来说不是必需的。
Overall, Django lets you create forms complete with error messages with very little HTML. If you add or remove fields from the User model, you don’t have to change any HTML.
总体而言,Django使您可以创建带有很少HTML的错误消息的表单。 如果您在User模型中添加或删除字段,则无需更改任何HTML。
不好的地方: (The not so good bits:)
1.虚拟环境 (1. Virtual environments)
The one place where Node has a clear advantage is the Node Package Manager (npm). When you clone a project from GitHub, you can run ‘npm install’ and get going straight away.
Node具有明显优势的一个地方是Node Package Manager(npm)。 当您从GitHub克隆项目时,可以运行“ npm install ”并立即开始。
Python handles dependency management differently using something called a ‘virtual environment’. The virtual environment is there to ensure you have the right version of each package and that they don’t get mixed up between projects.
Python使用称为“虚拟环境”的方式以不同方式处理依赖关系管理。 那里的虚拟环境可确保您拥有每个软件包的正确版本,并且不会在项目之间混淆它们。
The package manager for Python is called ‘pip’. You can use it to create a virtual environment, install packages, and generate a requirements file. The functionality is all there but the process for getting everything set up isn’t quite as slick as npm.
Python的软件包管理器称为'pip' 。 您可以使用它来创建虚拟环境,安装软件包并生成需求文件。 功能已经存在,但设置所有内容的过程并不像npm那样精巧。
It’s not a deal-breaker. When using Django for the first time, expect to spend a couple of hours getting your head around virtual environments. I used this course by Reindert-Jan Ekker. It’s very comprehensive and is less than 90 minutes long. Many beginner Django tutorials will also include a section on virtual environments.
这不是一个破坏交易的行为。 首次使用Django时,请花几个小时来熟悉虚拟环境。 我用这门课程由Reindert-JAN Ekker。 它非常全面,不到90分钟。 许多初学者的Django教程还将包括有关虚拟环境的部分。
2.需要定义自定义用户模型 (2. The need to define a custom user model)
Django provides a number of classes that can be extended to your needs, many of which are related to user management.
Django提供了许多可以扩展到您的需求的类,其中许多与用户管理有关。
The drawback is even Django’s documentation recommends you use a custom user model instead. The base user model provided by Django is very bare-bones and difficult to modify once a project is deployed. Creating a custom user model allows you to have users sign in with an email address instead of a username and allows you to add fields that aren’t related to authentication.
缺点是,甚至Django的文档也建议您使用自定义用户模型。 Django提供的基本用户模型非常简单,一旦部署了项目,就很难修改。 创建自定义用户模型后,您可以让用户使用电子邮件地址而非用户名登录,并可以添加与身份验证无关的字段。
Django has a full example in their documentation. I was able to copy and paste the code, modify a couple of fields, and get up and running very quickly. This has had a big influence on how I build projects. I find it best to create your user model before creating any models that might reference it.
Django在其文档中有完整的示例。 我能够复制和粘贴代码,修改几个字段,并很快启动并运行。 这对我构建项目的方式产生了很大的影响。 我发现最好在创建任何可能引用它的模型之前创建您的用户模型。
3. Django主义 (3. Django-isms)
Django’s documentation is relatively easy-to-read and comprehensive, but it still takes time and experience to find which parts of development have shortcuts (i.e. a class-based view). Creating your views and URLs often relies on importing a number of Django packages.
Django的文档相对容易阅读和全面,但是要找到开发的哪些部分具有快捷方式(即基于类的视图)仍然需要时间和经验。 创建视图和URL通常依赖于导入许多Django包。
‘Classy Class-Based Views’ is a good website for finding out where you can use built-in classes. Awesome Django has a well-curated list of resources.
“基于类的经典视图”是一个不错的网站,可帮助您了解可在哪里使用内置类。 很棒的Django有精心策划的资源列表。
Like any new framework, it’s something that takes time. This is why I think good quality courses and tutorials are worth the effort. These will provide a good source of code snippets.
像任何新框架一样,这需要时间。 这就是为什么我认为优质的课程和教程值得付出努力的原因。 这些将提供很好的代码片段来源。
我现在坚持使用Django (I’m sticking with Django for now)
Django’s main strength is its ability to achieve more with fewer lines of code. I have seen my productivity increase using Django, particularly for CRUD-based tasks using class-based views.
Django的主要优势在于能够用更少的代码行实现更多目标。 我已经看到使用Django可以提高生产率,尤其是对于使用基于类的视图的基于CRUD的任务。
To make the most of Django’s forms, I keep front and back-ends coupled. This means React is not an option. Vue, however, can be integrated with Django’s templates for more control on the front-end. React enthusiasts do have the option of using Django to create an API instead of a full-stack application.
为了充分利用Django的形式,我将前端和后端结合在一起。 这意味着React不是一个选择。 但是,Vue可以与Django的模板集成,以在前端进行更多控制。 React爱好者确实可以选择使用Django创建API,而不是使用全栈应用程序。
I would still advise caution for developers learning Django as their first full-stack framework. As Django does a lot for you, it’s possible to build apps without understanding some core principles under the hood.
对于仍将Django作为第一个完整堆栈框架学习的开发人员,我仍然建议谨慎。 由于Django为您做了很多事情,因此可能在不了解某些核心原理的情况下构建应用程序。
翻译自: https://medium.com/dev-genius/django-explained-for-javascript-developers-c0efaac269ac