This Python peculiarity can lead to some unexpected behavior of your programs.

Say, you want a function that appends some data to a list. And if the list is not passed, then appends the same data to a newly created list.

What would be the obvious and naive way to go? Sure, why not use default arguments here? Let’s have a look at the code:

def append_10(the_list=[]):
the_list.append(10)
return the_list
my_list = [5]
print(append_10(my_list))
print(append_10())


Passing a QR code as an SVG code

This tiny project will generate a QR code for any text and pass it to the HTML template as an SVG code.

SVG is a very convenient medium for URLs or other text. This small demo demonstrates a few things:

  1. How to pass a tag to the HTML
  2. How to generate a QR code
  3. How to use a variable as a stream (to write the SVG code not to the file but to the variable)
  4. How to use Bootstrap :) Just kidding.

The Tutorial

  1. pip install django
  2. pip install qrcode
  3. Create a Django project…


Learn by doing

We will write a Sales Orders Management System using Django REST for the back-end, and React JS for the front-end. We will add authentication with JWT and will cover the back-end with tests.

We will also use the CDN version of React JS, not the “create-react-app”. This will simplify the development, as we WILL NOT need to run two servers: one for Django and one for React.

Of course, we will be using Functional Components in React.

Bonus: Sweet Alerts for beautiful animation :)

Takeaways: complete project source code on GitHub

Contents:

  1. Create a Python Django Project


How much faster is your app with indexes?

A database index is like a phone book

Even “Senior” developers often forget to use indexes in Django. You can’t imagine how faster it can get if you simply add two lines of code into your model!

In this article we will take measurements together: with and without indexes.

Setup

Our patient will be a small Django application with just 7 fields.

I will just show the model here:

class Student(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
code0 = models.CharField(max_length=100, blank=True)
code1 = models.CharField(max_length=100, blank=True)
code2 = models.CharField(max_length=100, blank=True)
code3 = models.CharField(max_length=100, blank=True)
code4 = models.CharField(max_length=100, blank=True)

We will be testing…


Step by step guide to writing Tetris in Python with PyGame

In this tutorial, we will write a simple Tetris using the PyGame library in Python. The algorithms inside are pretty simple but can be a little challenging for the beginners. We will not concentrate on PyGame mechanics too much, but rather focus on the game logic. If you are too lazy to read all the stuff, you may simply copy and paste the code in the end.

Tetris Game

Prerequisites

  1. Python3. This may be downloaded from the official website.
  2. PyGame. Go to your command prompt or terminal, depending on the OS you…


There are things in Django that just work and you never worry about how exactly. One of these things is Sessions.

You have multiple users visiting your website. Some are logged in, some are just guests. You only need to access request.user to see if the current user is logged in or not. But what is happening under the hood?

Sessions

For each user, there is a session created.

A session is just another row in a table called django_sessions in your database that looks as on the image below:

So it contains only 3 fields: session_key, session_data, expire_date.

Every session…


I write programs for more than 30 years now, and for more than 15 years I teach programming in universities. I remember how I was learning to program 30 years ago, it was a completely different experience. Today, programming is much more effective, but if you are learning to program, then it’s hell.

So, what is different? And why you cannot learn it the way it was 30 years ago?

BASIC programming language

The first microcomputer version of BASIC was co-written by Bill Gates, Paul Allen and Monte Davidoff for their newly formed company, Micro-Soft, says Wikipedia.

Basic was a standard for all…


How to generate PDF reports in Django

In this tutorial, we will generate a PDF report and serve it to the front end.

Create a simple Django Project

We start by creating a Django project:

// create a Django project named pdfreport:
django-admin startproject pdfreport
// go to the new folder
cd pdfreport
// create a new environment
python3 -m venv venv
// activate this environent
source ./venv/bin/activate
// create an App for our project
python manage.py startapp app
// Install Django and FPDF2 in this environment
pip install django
pip install fpdf2

First, we want to use HTML templates, so…


Server-side rendered charts without API complications

What makes your Django project more beautiful? Charts!

In this tutorial, we will pass some statistical data to the front-end and draw the charts. We will not make the project complicated by adding any API, we will pass the data directly to JavaScript from the server.

Setup

We start by templating one of my GitHub repo that was mentioned in my other article https://github.com/timurbakibayev/django_bootstrap_admin

By doing so, we immediately get a beautiful bootstrap template with login functionality. Chart.js is already included in the package.

After templating it, we need to:

pip install -r requirements.txt --upgrade
python…


This will guarantee your success

I am a professor at the university, and students often ask me, how to become a Senior Developer. The answer is complicated, but there are 3 Tools that help you achieve this.

1. Brain

Timur Bakibayev

Professor, Ph.D. in CS, Almaty Management University

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store