I have it installed on Google Cloud Platform when I work. In Django's Hello World, I think that it is often done on localhost, but it is written to describe the flow of deployment. As a premise, it is assumed that Draw hello world with mod_wsgi is performed. (Because mod_wsgi is required)
environment OS: Debian 9.0 stretch Python: 3.6.3
I will install it immediately. It is assumed that Python is already installed. First, upgrade pip and install Django.
pip install --upgrade pip
pip install django
... is it really installed? Type the following in order.
python
>>> import django
>>> django.get_version()
'2.1.5'
If you can confirm the version like this, the installation is successful.
Then type `quit ()`
to exit python mode.
Go to your home directory and create a Django Project. (It doesn't have to be your home directory)
cd ~
django-admin startproject myproject
This will create a Django project directly under your HOME directory.
Modify the apache2 configuration file (sites-available / 000-default.conf) so that wsgi.py in Django responds to access to the document root.
cd /etc/apache2/sites-available
sudo vim 000-default.conf
documentroot
Comment out(Like this →# documentroot /var/www/html
)please.
If you have set `WSGIScriptAlias``` in [Draw hello world with mod_wsgi](http://qiita.com/shigechioyo/items/2b25f60918be6b81581a), please delete it as well. Then add the following: Don't forget to change the USERNAME part By the way, in vim you can replace it with
`:% s / USERNAME / YOURNAME / g```.
WSGIScriptAlias / /home/USERNAME/myproject/myproject/wsgi.py
WSGIPythonPath /home/USERNAME/myproject
<Directory /home/USERNAME/myproject/myproject>
<Files wsgi.py>
Order deny,allow
AllowOverride None
require all granted
</Files>
</Directory>
As the file name suggests, change the settings related to Django. Here we edit the file with vim.
cd ~/myproject/myproject
vim settings.py
First, we need to set the hosts that can be accessed, so change ALLOWED_HOSTS = [].
ALLOWED_HOSTS = ["*"]
Then change the language and time settings.
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
So far, you've created a Django project. Next, create a Django application.
Use manage.py
as usual.
cd ~/myproject
python manage.py startapp myapp
Then, the following hierarchical structure should be completed.
myproject
|
├── myapp
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── manage.py
└── myproject
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
Next, edit settings.py
to make the
myapp`` application recognized.
cd ~/myproject/myproject
vim settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp'
]
By adding myapp
in this way, the created application will be recognized.
Create the drawing part of the screen. Editing views.py in the myapp folder.
cd ~/myproject/myapp
vim views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django World")
Create the logic for which view to return for the client-side request url.
cd ~/myproject/myproject
vim urls.py
from django.contrib import admin
from django.urls import path
from myapp.views import home
urlpatterns = [
path('admin/', admin.site.urls),
path('', home),
]
Finally restart apache2.
sudo service apache2 restart
This should draw * Hello Django World *. Next, I plan to change the database to PostgreSQL.
Recommended Posts