How to install Django on your hosting

14 de septiembre, 2016 @ 13:56 — 0 comentariosDownload PDF

Although this guide is valid for any django installation, it is intended for users, especially with regards to file paths and applications

1 - Create the application on Hosting panel

To install Django app the first step is to create the python application from the hosting panel

The name you type as application name will be your Django app name (e.g., myapp)

Python definir aplicación

The choice of name myapp means that the directory of your Django application will be python/myapp

2 - Install Django package

To install the last version of Django launch this command through SSH remote shell:

pip install django --user

Test Django installation by typing this command:

python -m django --version

3 - Create Django project

As we advance, the directory of your application should be python/myapp. Therefore , to create the application you must run the following via SSH:

cd python
rm -fr myapp
django-admin startproject myapp

4 - Setup WSGI file

Assuming the user name of your hosting is cpanel_user and since the application is myapp , create the file myapp/ with the following content:

import sys

## Necesario con ambos casos. 
sys.path.insert(0, "/home/cpanel_user/python/myapp")

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

5 - Django setup

Now we need to edit the file python/myapp/myapp/ and set the static paths as follows:

STATIC_ROOT= '/home/cpanel_user/python/myapp/public/'

On the static files are mapped against the public directory that you place inside your application. (e.g., /home/cpanel_user/python/myapp/public)

Una vez establecidas las directivas anteriores, sólo debes usar la herramienta de copiado de archivos estáticos al directorio establecido:
Once established the previous directives , you only must use the static copy tool to set files in the directory :

cd myapp
python collectstatic

It will copy automatically the files to the apps public directory. It is advisable you execute this tool on every new deploy of your app.

6 - First "hello world"

Create the file python/miapp/myapp/ with the first basic view:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello world.")

And then we map the URL on python/myapp/miapp/ file:

from django.conf.urls import include,url
from django.contrib import admin
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),

Having done that, once we open our domain web site will see our first django "hello word" app

7 - Database setup

For this example will use Mysql Database, although in the is also PostgreSQL available .

First, will install the mysqlclient Python package:

pip install mysqlclient --user

then will setup our MySQL connection data in your app file:

 'default': {
 'ENGINE': 'django.db.backends.mysql',
 'NAME': 'database_name',
 'USER': 'database_user_name',
 'PASSWORD': 'password',
 'HOST': 'localhost',
 'PORT': '3306',

Now, we only need to create django migrations:

python migrate

8 - Setup superuser access

Launch the folowing command:

python createsuperuser

Now, we can login the administration web of the project (e.g.,

9 - Create the first app inside the project

To create an application within an existing project we will use the startapp option . For example with the following command :

python startapp new_app

this will create a new_app folder on the project directory. As in the main aplication, will create a and a with the the content of the default views

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. This is new_app.")
from django.conf.urls import url

from django.conf.urls import url, include
from django.contrib import admin

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),

Y finalmente mapeamos la url que queramos a la nueva aplicación, en el archivo de la aplicación principal:

urlpatterns = [
    url(r'^new-app/', include('new_app.urls')),
    url(r'^$', views.index, name='index'),