Search for:
Installing and Configuring Django Web Framework with Virtual Environments in CentOS/Debian – Part 1

Installing and Configuring Django Web Framework with Virtual Environments in CentOS/Debian &-8211; Part 1 &-8211; this Article or News was published on this date:2019-05-28 18:12:34 kindly share it with friends if you find it helpful

“This article is revised and updated with latest version of Django – May 2016”

Some 20 years ago when the World Wide Web was still in its infancy, having a personal or business web site was almost a rare luxury. With the subsequent development of several web technologies and the introduction of dynamic content provided by the combination of server-side programs and databases, companies could no longer be satisfied with having a static web site.

Setup Django Web Framework in CentOS DebianSetup Django Web Framework in CentOS Debian

Install and Configure Django in CentOS and Debian – Part 1

Thus, web applications became a reality – programs in the full sense of the word running on top of a web server and accessible through a web browser.

To make development easier and more effective, web frameworks were designed to aid programmers in their efforts to create applications. In short, a web framework takes care of common core functionalities in the development process such as dealing with user session management, interaction with databases, and the good practice of keeping business logic separate from display logic, to name a few examples.

In this 3-article Django series, we will introduce you to Django, a popular web framework based on Python. For that reason, at least a little familiarity with this programming language is suggested but if you have little to no experience with it, we’ll also walk you through the basics.

Part 1: Installing and Configuring Django Web Framework with Virtual Environments in CentOS/Debian

Installing Django in CentOS and Debian Servers

Although you can install Django from both the Debian (v1.7.7: extended support will be discontinued on December 2015) and Fedora EPEL (v1.6.11: extended support was discontinued on April 2015) repositories, the available version is not the latest stable LTS (Long Term Support) release (v1.8.13, as of May 2016).

In this tutorial we will show you how to install Django v1.8.13 since its extended support is guaranteed until at least April of 2018.

The recommended method to install Django is through pip, a popular tool for managing Python packages. Also, in order to create isolated Python environments and avoid conflicts between projects that may require different versions of software dependencies, the use of virtual environments is highly encouraged.

The tools that are used to create and manage virtual Python environments is called virtualenv.

Follow these steps to perform the installation:

1. For Fedora-based distributions (except in Fedora itself), enable the EPEL repository first:

- yum update && yum install epel-release

2. Install pip and virtualenv:

Fedora-based distros:
- yum install python-pip python-virtualenv
OR 
- dnf install python-pip python-virtualenv
Debian and derivatives:
- aptitude update && aptitude install python-pip virtualenv

3. Create a directory to store your initial project.

- mkdir ~/myfirstdjangoenv
- cd ~/myfirstdjangoenv

4. Create and activate a virtual environment:

- virtualenv myfirstdjangoenv

The above command creates a bunch of files and subdirectories into ~/myfirstdjangoenv and basically installs a local copy of Python and pip within the current working directory. Next, we need to activate the virtual environment we just created:

- source myfirstdjangoenv/bin/activate

5. Notice how the command prompt changes after the last command. It’s now time to install Django:

Note that the below screenshot was taken during a previous version of this tutorial, but the expected output is the same when installing Django 1.8.13):

- pip install Django==1.8.13
Setup Django Web Framework in CentOS DebianInstall Django in Linux

Install Django in Linux: CentOS and Debian

You can check the Django version by launching a Python shell from your current working directory:

- python
>>> import django
>>> print(django.get_version())

(Again, the above command should return 1.8.13 when checking the current Django version).

To exit the Python prompt, type:

>>> exit() 

and press Enter. Next, turn off the virtual environment:

- deactivate
Setup Django Web Framework in CentOS DebianDeactive Django Virtual Environment

Deactive Django Virtual Environment

Please note that while the virtual environment remains deactivated, Django is not available:

Setup Django Web Framework in CentOS DebianDjango Error

Django Error

How to Create a First Project in Django

To create a project within the virtual environment we created earlier, it needs to be activated:

- source myfirstdjangoenv/bin/activate

Next, the framework will create the entire directory structure to store your project. To do this, you will need to run.

- django-admin startproject myfirstdjangoproject

The above command will create a directory named myfirstdjangoproject inside your current working directory.

where you will find a file named manage.py (an utility that will help you manage your project later on) and another subdirectory (~/myfirstdjangoenv/myfirstdjangoproject/myfirstdjangoproject). This last subdirectory will serve as the container for the project files.

While the rest of the files will make real sense after we have reviewed some Python to start writing a real web application, it is worth and well to take note of the key files that will be found inside a project container directory:

  1. myfirstdjangoproject/__init__.py: This empty file tells Python that this directory should be considered a Python package.
  2. myfirstdjangoproject/settings.py: Specific settings for this Django project.
  3. myfirstdjangoproject/urls.py: a TOC (Table Of Contents) of your Django-powered site.
  4. myfirstdjangoproject/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.
- ls 
- ls -l myfirstdjangoproject
- ls -l myfirstdjangoproject/myfirstdjangoproject
Setup Django Web Framework in CentOS DebianCreate Django Project

Create Django Project

In addition, Django has a lightweight built-in web server (written in Python similar to Python SimpleHTTP, what else?) that can be used to test your applications during the development process without having to deal with the task of setting a web server at this particular stage.

However, you need to know that this is not suitable for a production environment – just for development. To launch your newly created project, change your current working directory to the container directory for your project (~/myfirstdjangoenv/myfirstdjangoproject) and run:

- python manage.py runserver 0.0.0.0:8000

If you run into the following error:

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

Do what it says:

- python manage.py migrate

and then start the server again:

- python manage.py runserver 0.0.0.0:8000

We will cover the concept of migrations in the next articles of this series, so you can disregard the error message for the time being.

In any event, you can change the default port where the built-in web server will be listening. By using 0.0.0.0 as the network interface to listen on, we allow other computers in the same network to access the project user interface (if you use 127.0.0.1 instead, you will only be able to access the UI from localhost).

You can also change the port to another one of your choosing, but you’ll also need to make sure that traffic through such port is allowed through your firewall:

- firewall-cmd --add-port=8000/tcp
- firewall-cmd --permanent --add-port=8000/tcp

Of course, it goes without saying that you will need to update the allowed port if you choose to use a different one while launching the lightweight web server.

You should see the following output in your terminal:

- python manage.py runserver 0.0.0.0:8000
Setup Django Web Framework in CentOS DebianStart Django HTTP Server

Start Django HTTP Server

At this point, you can open your favorite web browser and navigate to the IP address of the machine where you installed Django followed by the port number. In my case, it is a Debian Jessie box with IP 192.168.0.25 and listening on port 8000:

http://192.168.0.25:8000
Setup Django Web Framework in CentOS DebianDjango HTTP Server

Django HTTP Server

While it’s a great thing that we were able to complete the initial setup of a project, there is still much work left to do, as indicated in the message above.

Summary

In this guide we have explained how to install and configure a virtual environment for Django, a versatile open source web framework based on Python.

Regardless of whether you are an application developer or a system administrator, you will want to bookmark this article and the rest of this series because chances are that at some point or another you will need to consider the need of such tool for your daily tasks.

In the following articles of this series we will discuss how to build on what we have already accomplished to create a simple, yet functional, web application using Django and Python.

As always, don’t hesitate to drop us a note if you have questions about this article or suggestions to improve. We look forward to hearing from you!

Reviewing Python Basics and Creating Your First Web Application with Django – Part 2

Reviewing Python Basics and Creating Your First Web Application with Django &-8211; Part 2 &-8211; this Article or News was published on this date:2019-05-28 18:12:20 kindly share it with friends if you find it helpful

“This article is revised and updated with latest version of Django – May 2016”

As we briefly commented on the last article of this series, Django is a free and open source web framework that turns application development into a faster task done in a more effective way – from the programmer’s point of view.

Installing and Configuring Django Web Framework with Virtual Environments – Part 1

Create Web Applications Using DjangoCreate Web Applications Using Django

Create Web Applications Using Django – Part 2

To do so, Django follows the MVC (ModelViewController) design pattern, or as their FAQs state, it can better be described as a MTV (ModelTemplateView) framework.

In Django, a “view” describes which data is presented to the user, whereas a template describes how the data is presented. Finally, the model is the source of information about data in the application.

In this article we will review some Python basics and explain how to prepare your environment to create a simple web application in the next tutorial.

Learn Some Python Basics

As an object-oriented programming language, Python organizes things into a collection of objects with properties (also known as attributes) and methods (also known as actions). This allows us to define an object once and then to create multiple instances of such objects with the same structure of properties and methods without having to write everything from scratch every time. Objects are thus defined by classes that represent them.

For example, a Person object could be defined as follows:

Properties:
  1. Person.height
  2. Person.weight
  3. Person.age
  4. Person.ethniticity
Methods:
  1. Person.eat()
  2. Person.sleep()
  3. Person.walk()

As in most programming languages, a property is defined by the object’s name followed by a dot and the attribute’s name, whereas a method is indicated in the same fashion but also followed by a pair of parentheses (which may be empty or not – in the latter case, it may contain a variable upon whose value the method will act, such as Person.eat(cake) or Person.sleep(now), to name a few examples).

To define methods in Python, you will use the def keyword, followed by the method’s name and a set of parentheses, with an optional object as you will see in a minute.

All of this will become much clearer during the next section where we will dive into a real example.

Creating the structure of a web application

As you may recall from Part 1 of this Django series, we said that a web application requires a database to store data. When you create an app, Django automatically sets up a Sqlite database that works just fine for small to middle size applications, and is what we will use in this case to store data for a classic first-time web app: a blog.

To start a new application inside of a project (by the way, you can think of a project as a collection of web applications), run the following command after activating the virtual environment we set up in Part 1 of this series.

- cd ~/myfirstdjangoenv/
- source myfirstdjangoenv/bin/activate
- cd ~/myfirstdjangoenv/myfirstdjangoproject
- python manage.py startapp myblog
Create Web Applications Using DjangoCreate Web Application Project in Django

Create Web Application Project in Django

Note that you can change the app’s name (myblog) for a name of your choosing – this is only an identifier for the application (please note that all management tasks are invoked using the manage.py script via the python binary – feel free to explore its source code if you have a minute):

Now let’s go inside the inner myfirstdjangoproject directory and find the file settings.py, where we will tell Django to use myblog as an application:

- cd ~/myfirstdjangoenv/myfirstdjangoproject/myfirstdjangoproject
Create Web Applications Using DjangoMy Django Web Project

My Django Web Project

Look for the INSTALLED_APPS section and add myblog inside single quotes as shown below:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myblog'
)

(By the way, the lines beginning with django above represent other Django applications that are activated in the current project automatically when it is first created and are supposed to aid the developer in writing code related to administration, authentication, content type declarations, and so on, in his / her application).

Thus, myblog will become activated, along with the other built-in applications, in this Django instance.