Skip links

Jody Boucher

Software Engineer

Tag

5 posts tagged with python

Django Middleware: An overview of middleware

The Django web framework contains hooks enabling a global mechanism for modifying the request and/or response in between the web-server interface and the view. Django middleware are the components that make use of these hooks.

An example of Django middleware is a component that adds a user attribute to each incoming HttpRequest.

This post provides an overview of middleware in the Django web framework.

[Read more…]about Django Middleware: An overview of middleware

Django How-To: Using the null and blank field options

Django logo

Models typically make up the foundation of most Django projects. They are the primary abstraction for managing the data/state that is the core of most "applications". Models not only define the structure of the database, each model is the mechanism for accessing and modifying the data stored in the database table represented by the model. Models are also generally responsible for any behaviors of the data and for validating data to protect the quality of the data in the database.

The most obvious part of any Django model is the list of fields. Each field in a model is related to a column or attribute in the database table associated with the model. Fields have two options that can be confusing to many developers that are new to the framework. The two options are null and blank. Although the two options might seem to address similar functionality, they are used for very different purposes.

[Read more…]about Django How-To: Using the null and blank field options

Django project layout alternatives

Django logo

Laying out and organizing a development project (Django or otherwise) is a task to which many people do not give much consideration, typically deferring to some default layout. This default layout is usually obtained in one of several ways:

  • using Django's admin tool (django-admin.py startproject) to create a new project
  • copying an old project and using it as the basis of the new project
  • cloning a project found on GitHub or elsewhere and stripping it down to its bones

Most developers won't stop to think about the project layout and whether a different alternative may work better. Many don't even realize that there are different ways you can layout a Django project.

[Read more…]about Django project layout alternatives

Update the version of Python in a virtualenv

What is virtualenv

It's pretty typical to do Python coding within a virtual environment. The virtual environment isolates any Python packages/dependencies to that environment so they do not impact your system level packages or packages in another virtual environment. It solves the common dilemma of maintaining dependencies when “Project A depends on version 1.x but Project B depends on version 2.x”. You can set up a different virtual environment for each of your Python projects, each with different package dependencies or different versions of the same packages. Or you can set up a new virtual environment to test how your project will work with different package versions. virtualenv is a tool you use to create these isolated Python environments. virtualenvwrapper is a set of extensions for virtualenv designed to simplify working with virtual environments.

The problem with updating Python in a virtualenv

A glaring issue with virtualenv is that although it is quite simple to install/remove/update or otherwise maintain any Python packages installed in the virtual environment, virtualenv provides no built in way to upgrade or change the version of the Python interpreter installed in the virtual environment.

[Read more…]about Update the version of Python in a virtualenv