{{announcement.body}}
{{announcement.title}}

Tips for Beginning Backend Web Developers to Advance Quickly

DZone 's Guide to

Tips for Beginning Backend Web Developers to Advance Quickly

U + Django = <3

· Web Dev Zone ·
Free Resource

handler-holding-python-up-to-face

U + Django = <3

It’s easy to learn to build scalable interactive web sites if you choose a simple learning path. I recommend you start with Python and Django instead of JavaScript and Mongo, Express, Angular (or React, Vue) and Node (MEAN) stack.

I've observed the success and failure to approaches that thousands of developers took to access a REST-like API in the RICOH THETA developer community. I saw problems students encountered in workshops and one-on-one training. Novice backend developers finish more projects when they use Python. To get people started quickly, I developed a free video course on YouTube, The Beginner Django Tutorial.

Although these tips and the free video course can be used by people with no prior experience, they are designed for people that know the following: HTML, CSS, JavaScript, Bootstrap.

You may also like: Tutorial for a First Project Using Django.

Python

Initially, it's attractive to use JavaScript on the backend since the same developer is using JavaScript on the frontend. For the beginner backend developer, this is a mistake.

Despite the media attention on hot JavaScript backend frameworks and the perceived shortcomings of aging Python frameworks, you will enjoy more deployment success with Python. After you understand the basic concepts with Django, you can apply them to a JavaScript stack.

JavaScript is surprisingly difficult to use properly for your first project developing a backend application for commercial deployment. Due to the rapid evolution of JavaScript, you’ll likely see backend examples that use JavaScript features such as closures, prototype chains, inheritance, concurrency, advanced async patterns and the ambiguous use of the ‘this’ keyword.

You will not be able to stick to basic JavaScript syntax and complete your backend project. Even a beginner node tutorial is likely to deal with asynchronous I/O and prototypes.

If you're not already using JavaScript build tools for your frontend development, you'll also need to learn some type of bundler, such as Webpack and task runner.

Even if we disregard the problem of rapid JavaScript evolution and huge choices in build tools, Python concepts and syntax are likely to be more familiar and easier to learn. Many people took a beginner programming course in high school or college that used Java or Python. For these people, Python classes will be much more comfortable than using JavaScript classes.

Example of Python class:

from django.db import models


class Thetaimage(models.Model):
    image = models.ImageField(upload_to='images/')
    title = models.CharField(max_length=80)
    summary = models.CharField(max_length=160)

This is from the GitHub repo for The Beginner Django Tutorial models.py file. This is the class that connects the SQL server to the Django views.py file. Even with no knowledge of Python, you can understand that the Thetaimage class extends the models.Model class.

You get the data from the model into web page with the views.py file.

from django.shortcuts import render
from django.http import HttpResponse
from .models import Thetaimage


def home(request):
    images = Thetaimage.objects
    return render(
        request,
        'theta_images/home.html',
        {'images': images})


In the example above, line 3 imports the Thetaimage class from the previous snippet. Remember that Thetaimage holds all the info from the SQL database. In line 7, images = Theta.objects , we store all the objects in the database into a variable.

We are building a 360 image gallery in this example. Each image is an object with attributes for title , image , and summary . Line 11 passes all the objects to a HTML template.

If you’re already an expert JavaScript developer, you should use JavaScript on the backend. If you're getting started learning intermediate and advanced JavaScript syntax and patterns, consider postponing your advanced JavaScript studies and check out Python.

Django

Django takes the "everything included" approach. The database, admin interface, authentication, user management, and web server work as soon as you start a project. This "all-in" approach is great for beginners. Django is also an opinionated framework. It locks or guides you toward one way of doing things. While this limits the choices for advanced developers, it means that beginning backend developers can get started in minutes.

As an example, the Django admin application comes with login capability.

Django login landing page

Django login landing page


It also provides a way to upload and manage content.

Managing and uploading content with Django

Managing and uploading content with Django


The admin.py file provides an easy way to register your Django applications.

The entire code for the file is shown below:

from django.contrib import admin
from .models import Thetaimage

admin.site.register(Thetaimage)


Pipenv

Use a virtual environment and a Python package installer for every project. Do not install the Python packages into your main system. If you're familiar with JavaScript, you're likely using npm for package management and n for node version management. In Python development, people use virtualenv for Python version management (the version of Python to use) and pip for package management.

You can also use Anaconda and conda. However, as I believe that beginners learn best with one recommendation and not a multitude of choices, I'll recommend pipenv. It manages both pip and virtualenv through a single command line interface.

pipenv offers pip and virtualenv all at once

pipenv offers pip and virtualenv all at once


A short video explaining how to install pipenv is available here.

The basic process is:

  1. Install Python 3 — you can install Python from python.org or on Ubuntu 18.04 Linux with sudo apt install python3.7
  2. Install pipenv with python3 -m pip install pipenv
  3. Install your packages with pipenv. Example: pipenv install django
  4. Activate the virtual environment with pipenv shell

Model View Template

Most people have heard about the Model-View-Controller (MVC) paradigm. Although Django follows this paradigm, it can be a bit confusing. The Django views.py file functions as the Controller, not the view. The templates files handle the view. A paradigm that is easier to understand is Model-View-Template or MVT.

MVT architecture

MVT architecture

Every Django application uses these components:

  1. Models.py holds data structure.
  2. Views.py holds the Python logic that accesses the data from models.py , processes it, then sends it to the HTML files in the templates directory.
  3. Templates are HTML files with embedded Python variables and code.

A short five minute video explaining MVT is available here. I explain MVT by showing the path from the data in the database to the Python code in views.py. Then, I move to the HTML file in the templates. This is the developer's perspective.

If you start from the web browser or mobile phone, the path is different.

Path starting from web browser

Path starting from web browser

If you start from the browser, the path first hits the urls.py file which routes it to the Python code in views.py. The Python code imports the data from the models.py file and sends the data to the HTML template. A code walkthrough showing how to connect the model, view, and the template is available here.

While Django does follow an MVC pattern, it's easier to learn by thinking of an MVT paradigm. If you move from Django to a MEAN stack, the MVC and MVT concepts will be easy to transfer to your new stack.

Practice Using the Curly Brackets

Curly brackets, {{ shopping_cart.item }} , look weird in HTML. Don't be afraid of them. Double curly brackets contain a variable.

Curly bracket in HTML example

Curly bracket in HTML example

A curly bracket with a percent sign, {% for plane at airport %} , contains Python code that performs an action such as a loop.

Running Python in HTML

Running Python in HTML

This is how the curly brackets look in HTML.

Image title

This concept of embedding programming code into HTML exists in all backend web frameworks.

Summary

Python and Django are easier to learn for the beginner backend developer today. The concepts you learn with Django can be transferred to a JavaScript stack. You'll also build confidence by deploying Django projects in a short period of time. JavaScript tooling is improving at a rapid rate, but it's still difficult to master for some people. If you've tried a MEAN stack and ran into difficulties, take a look at Django.


Related Articles

Topics:
backend, django, python

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}