Wednesday, March 28, 2018

Django: how to install (python3, apache)

Install essentials


I will explain how to install Django. I'm using Linux mint.
At first, we need to make a local virtual server and install necessary things.
See here.

If you want to use nginx instead of apache, see here.

Then we will install git.
$ sudo yum install -y git

Check if python3 and pip3 are installed.
$ python3.5 -m pip --version
pip 9.0.1 from /usr/lib/python3.5/site-packages (python 3.5)

We use pyenv. Why do we need pyenv/virtualenv?
$ cd /vagrant
$ pyvenv-3.5 djangoapp
If this doesn't work and can't create the folder, it's maybe because of the permission. Stop virtual machine (vagrant halt) and run CMD and PuTTy or Teraterm as an administrator. Then try these commands again.


Start the virtual environment of pyenv:
$ source ./djangoapp/bin/activate
If you want to stop the virtual environment:
$ deactivate
But you don't need to stop it yet.
Update pip of the virtual environment (pyenv) if necessary:
$ python3.5 -m pip install --upgrade pip

Because we are in the virtual environment (pyenv), the python and pip of virtual environement will be used:
$ python --version
Python 3.5.4
$ which python
/vagrant/djangoapp/bin/python
$ pip --version
pip 9.0.1 from /vagrant/djangoapp/lib64/python3.5/site-packages (python 3.5)
and we can  use/install/update/downgrade python and python's modules in this environment without affecting the system's python libraries. (If you need to care system's and django's python/python's module versions simultaneously... it would be boring but hard task. So we simply use pyenv and separate the environments.)

Sign in to mysql and create a table:
$ mysql -u root -proot
mysql> CREATE DATABASE testdb CHARACTER SET utf8;
mysql> exit

Install Django


In the pyenv virtual environment, install django.
$ python3.5 -m pip install django

You can check the version of django.
$ python3.5 -m django --version

Install uwsgi (WSGI: Web Server Gateway Interface):
$ python3.5 -m pip install uwsgi

Create a project of Django:
$ cd /vagrant/djangoapp
$ django-admin startproject mypj

After doing these commands, you will get a project folder of Django in this directory: /vagrant/djangoapp. The name of the project folder is "mypj". And in the mypj folder, you will find that there is another mypj folder there.


In the folder....



Open the file "setting.py" that is in the folder "mypj".


Add "localhost" and "192.168.33.10" to "Allowed_Hosts" like this:
ALLOWED_HOSTS = ['localhost','192.168.33.10']
(Assuming that your virtual server's IP address is "192.168.33.10")


Run this command and start the built-in server of Django:
$ python3.5 mypj/manage.py runserver 0.0.0:8000

Go to the URL: http://192.168.33.10:8000/



If this is displayed, it means you successfully installed Django :)

Hello World


Now we stop the built-in server with ctrl+c.

Go to the mypj directory on the console.
$ cd ./mypj

There is manage,py in the top directory of django

mypj project automatically created in the "mypj" directory is a submodule of "mypj". We will create a top-level module in the directory where manage.py is.
$ python3.5 manage.py startapp mywebsite

mywebsite directory is created

In the mywebsite directory, you can see that these files are there:


Open the view.py and write as follows:
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world Django!!!!")

Create a file called "urls.py".



Write as follows in urls.py:
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Now go back to djangoapp/mypj/mypj (the submodule of mypj) and open urls.py.
Import "include" and add this path.
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('mywebsite/', include('mywebsite.urls')),
    path('admin/', admin.site.urls),
]

Make sure you at the top directory of Django.

Run the server.
$ python3.5 manage.py runserver 0.0.0:8000

Go to http://192.168.33.10:8000/mywebsite/ and you will see the message you defined in index function.


Please note, when you deploy your django project to production, you can't use "runserver". Google "how to deploy django apache".