What is Django and why Django?
Django is a web framework of Python. Also I think Django is the most popular Python's framework.
Install dependencies
If you have followed my other posts, you should have python3.6 in your virtual CentOS7 machine:
$ python3.6 --version
Python 3.6.5
Install git:
$ sudo yum install -y git
We will use pyenv which is a virtual environment for Python (but not like virtual machines. See this stackoverflow for detail: Is pyenv or virtualenv essential for Django?
$ cd /vagrant
$ python3.6 -m venv djangoapp
$ ls
djangoapp index.py Vagrantfile
Start the virtual environment:
$ source ./djangoapp/bin/activate
Inside the virtual environment, you can use different version of python from the python installed in CentOS system (if needed). We will use simply Python3.6 installed in CentOS though.
(djangoapp) [vagrant@localhost vagrant]$ python --version
Python 3.6.5
(djangoapp) [vagrant@localhost vagrant]$ which python
/vagrant/djangoapp/bin/python
(djangoapp) [vagrant@localhost vagrant]$ pip --version
pip 10.0.1 from /vagrant/djangoapp/lib64/python3.6/site-packages/pip (python 3.6)
If you want to stop the virtual environment (But you don't need to stop it yet):
$ deactivate
We will create a database in mysql for Django.
$ mysql -u root -proot
mysql> CREATE DATABASE testdb CHARACTER SET utf8;
mysql> exit
Install Django
We will install Django from pip:
$ python3.6 -m pip install django
Check if Django was installed successfully:
$ python3.6 -m django --version
Configuration of Django
Create your first Django project
$ cd /vagrant/djangoapp
$ django-admin startproject mypj
$ ls
bin include lib lib64 mypj(<--Your project) pip-selfcheck.json pyvenv.cfg
Open the file "setting.py" as follows. And change the Allowed hosts values.
$ sudo vi ./mypj/mypj/settings.py
Allowed hosts (Assuming that your virtual server's IP address is "192.168.33.10"):
ALLOWED_HOSTS = ['localhost','192.168.33.10']
Save and close it by :wq.
Run this command and start the built-in server of Django:
$ cd /vagrant/djangoapp
$ python3.6 mypj/manage.py runserver 0.0.0:8000
Ctrl + c to stop the built-in server. Your Django is working here: http://192.168.33.10:8000/
If this is displayed, you successfully installed Django. Stop the built-in server by Ctrl + c after confirming this.
Hello World
Run the following commands to add "mywebsite":
$ cd /vagrant/djangoapp/mypj
$ python3.6 manage.py startapp mywebsite
$ ls
db.sqlite3 manage.py mypj mywebsite
$ ls ./mywebsite
admin.py apps.py __init__.py migrations models.py tests.py views.py
mywebsite was added in the directory of mypj.
Run the following commands to add a new page.
$ cd /vagrant/djangoapp/mypj/mywebsite
$ sudo vi views.py
Then add this inside:
def index(request):
return HttpResponse("Hello, world Django!!!!")
Now create a file named "urls.py":
$ sudo vi urls.py
And write as follows inside:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
We will add the path in the mypj:
$ cd /vagrant/djangoapp/mypj/mypj
$ ls
Open urls.py and add the path:
$ sudo vi urls.py
Write as follows:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('mywebsite/', include('mywebsite.urls')),
path('admin/', admin.site.urls),
]
Now run the server:
$ cd /vagrant/djangoapp/mypj
$ python3.6 manage.py runserver 0.0.0:8000
Go to: http://192.168.33.10:8000/mywebsite/ You will see Hello World:
Django production deployment
Don't use the built-in server for production deloyment. See the official website of Django for the deployment.