Configuring a Django Project to Use PostgreSQL

Django projects are configured by default to use a SQLite database, but the framework has built-in support for PostgreSQL as well, which is more suitable for production. If you haven't created your first Django project yet, see Creating and Setting Up a Django Project. Here's how to set up things to run your Django application with a PostgreSQL database.

First, make sure you have a PostgreSQL server running. The most quickly way to do this is via Docker. This command will launch a new container with a fresh PostgreSQL database:

docker run -p 5432:5432 --name oscardb -e POSTGRES_PASSWORD=postgres -d postgres

Read more…

Setting up an E-commerce Site With Django Oscar

Oscar is a collection of Django applications to create e-commerce sites with Python. It is well designed, extensible, and has plenty out-of-the-box features. You can run it as-is or use it as a framework to create a highly customized shop.

Personally, I have used Oscar to build tailor-made shops for my customers. Whatever your purpose is, the installation and setup process is the same. Since Oscar documentation might not be the most complete and up-to-date, I decided to write this introductory guide. Note that this is guide is for setting up Oscar in a local, development environment. A production deployment might require extra work and configuration, which is out of our scope, although this guide can work as a starting point.

Read more…

Notebook Widget (Tabs) in Tk (tkinter)

The Tk notebook widget (or tab panel) allows you to divide a part of the window into different tabs. Depending on which tab is selected, what the user views in the window changes. A paradigmatic example of this widget is the tabs in modern web browsers. The notebook widget was introduced in Tk 8.5. In Python, it is provided by the tkinter.ttk.Notebook class.

/images/notebook-widget-tabs-in-tk-tkinter/notebook-widget-tkinter.png

Read more…

Populating a Django Project With Test Data

As a Django developer, I usually want to remove all of my database's tables in order to start fresh and see how my application behaves when running for the first time. This is even more tempting when using SQLite, where it's just a matter of removing a single file. But if you do that regularly as well, you will find yourself creating test data (an admin user, a catalogue of products, a blog post) over and over. As a workaround, I have found that the most useful thing is to create a testdata application that populates my database when it runs for the first time (or, more precisely, when migrations run for the first time.)

To do so, start by creating a new application by running the following command in your project's directory (where manage.py is):

python manage.py startapp testdata

Read more…

What Is io.BytesIO Useful For?

io.BytesIO is a standard class that creates an in-memory binary stream, that is, it behaves like a file but exists only in our program's memory. This means you can read from and write to it just like a file, but without creating any actual files on disk. In Python, file-like objects are objects that implement methods like read(), write(), and seek(), allowing you to interact with data in a way similar to working with files.

How is BytesIO useful? Consider, for example, the following table of products stored in a Pandas DataFrame:

import pandas as pd
products = pd.DataFrame(
    columns=("name", "price"),
    data=(
        ("Keyboard", 20),
        ("Mouse", 5),
        ("Printer", 100),
        ("Headphones", 35.5)
    )
)

Read more…

Styling Widgets in Tk (tkinter)

You already have a desktop application written in Python with Tk, and now you want to make some changes to the appearance of the UI (buttons, labels, text boxes, etc.) By default, widgets in a tkinter application have a certain appearance, which in most cases is quite acceptable as it conforms to operating system standards. However, you have the possibility of altering the default appearance of any widget (e.g., changing the text color of a button from black to red). This can be done in two different ways.

Read more…

Creating and Setting Up a Django Project

Developing a new website in Django requires a series of initial steps that can be somewhat tedious and confusing when getting started with the framework: creating the project, setting up an application, configuring the URLs, etc. Here is a quick and easy to follow guide that can be used as reference every time you need to start a new project.

Read more…

Reproducing SQL Injection in sqlite3 and PyMySQL

SQL injection is a security vulnerability that allows an unauthorized user to execute SQL queries against a database. It can be exploited in any type of application (console, web, desktop, mobile). An application might be vulnerable to SQL injection when allowing the user to enter some data that is later included in a database query. In this post you will learn how to create and exploit vulnerable code in Python, using SQLite and MySQL as example databases. If you want to keep your Python application secure, you need to know how vulnerabilities arise!

Read more…

Scrollbar in Tk (tkinter)

The scrollbar is a Tk widget that allows you to modify the visible area (called viewport) of other widgets. A scrollbar can be vertical or horizontal, and is typically attached to widgets that display multiple elements, lines, or columns, such as listboxes (tk.Listbox), tree views (ttk.Treeview) or multi-line textboxes (tk.Text). In this post you will learn how to create and configure scrollbars.

/images/scrollbar-in-tk-tkinter/listbox-with-scrollbar.gif

Read more…