Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Using RHSCL: Django on Python 3 with PostgreSQL

June 24, 2013
Bohuslav Kabrda
Related topics:
LinuxPython
Related products:
Red Hat Enterprise Linux

Share:

    This article will show you how to use two software collections of RHSCL 1.0 Beta for cutting edge development. We will create a Django 1.5 application (running on Python 3.3), that will use PostgreSQL 9.2 as a database.

    Installing Dependencies

    First off, we will install the required collections. If you haven't done so already, you need to subscribe to the correct RHN channel (rhel-x86_64-variant-6-rhscl-1-beta, where variant is one of server, client or workstation).

    Now you should be able to install all the needed dependencies just by issuing this command (as a superuser):

    yum install python33 postgresql92 python33-python-psycopg2

    The reason why python33-python-psycopg2 has to be listed is that it doesn't belong to the basic python33 installation package set.

    Configuring PostgreSQL

    Now we need to setup a PostgreSQL database. First, let's initialize database cluster and run the service (as a superuser):

    service postgresql92-postgresql initdb
    service postgresql92-postgresql start

    Second, we need to create the actual database. For this, we have to switch to postgres user, who has permissions to do this, then we need to create new database user (name should be the same as the name of your local account) and then finally the database called testdb (again, do this as a superuser):

    su - postgres
    scl enable postgresql92 bash
    createuser
    createdb testdb --owner

    We're all set and we can continue with the Django part. Before going on, don't forget to exit the all the subshells to get back to your original shell.

    Creating a New Django Project

    We will create a new Django project using virtualenv. Everything we need (except for Django itself) is present in the python33 collection, so we just need to create the virtual environment and install Django from PyPi there:

    scl enable python33 postgresql92 bash
    virtualenv --system-site-packages my-django-app
    cd my-django-app
    source bin/activate
    pip install django==1.5.1

    To comment a bit on the above:  First we switch to SCL-enabled shell (don't forget to enable both collections), then we create a virtualenv directory my-django-app (passing the option to use RPM installed site-packages from the collection). After that, we switch to the newly created directory and activate the virtual environment (lines 3 and 4). Finally, we install Django (I decided to pin it to version 1.5.1 in this example, but you should be able to use any version >=1.5.0.)

    Now that everything is set, we can create the actual Django project:

    django-admin.py startproject myproj
    cd myproj

    Then we will adjust database settings to use our previously created database. In your favourite text editor, open myproj/settings.py and fill in database engine, user and name, so that the DATABASES section looks like this:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'testdb',
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
        }
    }

    ... and then run

    python manage.py syncdb

    to make sure that the database is set correctly. You will be asked whether or not you want to create a new superuser for your project - feel free to choose either option, it doesn't matter for this tutorial.
    And that's it! We now have a fully operational Django 1.5 application running on Python 3.3, using PostgreSQL 9.2 - all that on RHEL 6. Now we can just create a simple view that will print out all this.

    A Simple View to Prove It

    As an optional step, we will create a really minimalistic view that confirms we're using the right versions. First, let's define a new view function. Create a new file myproj/views.py and copy&paste this into it:

    import sys
    
    import django
    
    from django.db import backend, connection
    from django.shortcuts import render_to_response
    
    def index(request):
        return render_to_response('index.html',
                                  {'sysver': sys.version,
                                   'djangover': django.VERSION,
                                   'pgsqlver': backend.get_version(connection)})

    Then create a directory myproj/templates and a file myproj/templates/index.html (note, that we will not use actual HTML, but just a plain text to render the versions, which suites our example but would otherwise be considered very ugly):

    Python: {{ sysver }}, Django: {{ djangover }}, PostgreSQL: {{ pgsqlver }}

    Now we need to tell Django where to look for templates. Find TEMPLATE_DIRS constant in myproj/settings.py and replace it with following:

    import os
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    TEMPLATE_DIRS = (
        os.path.join(BASE_DIR, 'templates'),
    )

    Finally, let's route the / (root URL of our Django project) to the newly created view. Open myproj/urls.py in your favourite editor and insert following line into patterns:

    url(r'^$', 'myproj.views.index', name='home')

    Now just run python manage.py runserver and go to http://127.0.0.1:8000/ in your browser. You should see a result like this:
    Python: 3.3.2 (default, Jun 12 2013, 11:40:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)], Django: (1, 5, 1, 'final', 0), PostgreSQL: 90204

    Last updated: February 24, 2024

    Recent Posts

    • How to enable Ansible Lightspeed intelligent assistant

    • Why some agentic AI developers are moving code from Python to Rust

    • Confidential VMs: The core of confidential containers

    • Benchmarking with GuideLLM in air-gapped OpenShift clusters

    • Run Qwen3-Next on vLLM with Red Hat AI: A step-by-step guide

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue