Django Explained for JavaScript Developers

Alice Ridgway
Dev Genius
Published in
6 min readSep 14, 2020

--

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.

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.

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.

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.

Today, I’m sharing with you 6 good bits and 3 not so good bits about Django.

The good bits:

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.

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

For 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.

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.

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.

3. Databases

When you start a Django project, it comes already set up with a SQLite database.

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.

If you need something more powerful, setting up a different database like PostgreSQL is no harder than setting up Mongo DB for Express.

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.

In Node, controllers are written as functions. They take a request and return a template or a 404 error.

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.

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.

They take some getting used to, particularly understanding what’s going on under the hood, but they are brilliant for speeding up development.

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.

Instead, the syntax with dot notation will be very familiar to JavaScript developers.

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. Pain-free forms

For forms, Django passes the form object into the HTML template. This means you don’t have to create <input> 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.

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.

The not so good bits:

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.

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.

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.

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.

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.

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 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.

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.

‘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.

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.

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.

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.

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.

--

--