[PYTHON] Hello world instead of localhost in Django

Introduction

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

Django installation

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.

Create a Django project

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.

Change the apache2 configuration file

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

documentrootComment 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>

Modify settings.py in the django config file

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'

Create a Django Application

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.

Edit views.py

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")

Edit urls.py

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

Hello world instead of localhost in Django
Hello World on Django
Programming language in "Hello World"
Hello World (beginners) on Django
Let's do "Hello World" in 40 languages! !!
Implementation of login function in Django
cout << "Hello, World! \ N" in python
Hello World in Flask [Appropriate memo]
Code: 2 "Hello World" in "Choregraphe-Python script"
The meaning of ".object" in Django
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Hello world
R: Use Japanese instead of Japanese in scripts
(For myself) Django_1 (Basic / Hello World / Template)
How to display Hello world in python
Hello World with gRPC / go in Docker environment
Hello world with full features of Go language
Models in Django
Pymacs hello world
The story of viewing media files in Django
Create a one-file hello world application with django
To myself as a Django beginner (3)-Hello world! ---
Forms in Django
cython hello world
The story of building the fastest Linux environment in the world
Study from the beginning of Python Hour1: Hello World
Explanation of NoReverseMatch error in "python django super introduction"
web2py memo: Hello World
hello world with ctypes
Impressions of touching Django
RabbitMQ Tutorial 1 ("Hello World!")
Hello, World with Docker
Django's first Hello World
Hello world with flask
I participated in the translation activity of Django official documents
Display "Hello World" created in the local environment on the web
Summary of stumbling blocks in Django for the first time
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
Django Changed to save lots of data in one go