How to install Django on your hosting

14 de septiembre, 2016 @ 13:56 — 0 comentarios

Although this guide is valid for any django installation, it is intended for guebs.com 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/passenger_wsgi.py 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/settings.py and set the static paths as follows:

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

On guebs.com 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 manage.py 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/views.py with the first basic view:

from django.http import HttpResponse

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

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

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    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 guebs.com 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 settings.py file:

DATABASES = {
 '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 manage.py migrate

8 - Setup superuser access

Launch the folowing command:

python manage.py createsuperuser

Now, we can login the administration web of the project (e.g., http://your_domain.com/admin)

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 manage.py startapp new_app

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

views.py

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

urls.py

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 urls.py de la aplicación principal:

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