There is a library called Django that allows you to build web apps using Python. Recently, it seems that it is better to use PaaS than to rent a server when publishing Web services. So, as a review for myself, I will summarize the procedure from creating a homepage with Django, deploying it to Heroku, and publishing it. Also, for some reason, many of these web-based technical articles are for Mac, but this article is compatible with Windows.
Anyway, build a virtual environment. This article uses Anaconda, but pipenv can be anything. In the worst case, you can use the local Python environment as it is, but I don't recommend it because requirements.txt
will be great when you do pip freeze
later. There is a high possibility of moss when deploying.
C:\Users\hoge>conda create -n django-heroku python==3.7
C:\Users\hoge>conda activate django-heroku
(django-heroku) C:\Users\hoge\testproject>conda install django==2.2
The version of python is not so severe, so I think anything is fine, but somehow I specify 3.7
. Please see here for the supported versions.
(django-heroku) C:\Users\hoge\testproject>django-admin startproject testproject .
(django-heroku) C:\Users\hoge\testproject>python manage.py startapp testapp
Edit the part ʻINSTALLED_APPS of
C: \ Users \ hoge \ testproject \ testproject \ settings.py` as follows.
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testapp',#add to
]
At this stage you should already be able to see it on your local server.
(django-heroku) C:\Users\hoge\testproject>python manage.py runserver
If you connect to http://127.0.0.1:8000/, it will be as follows.
I will display the characters on this.
Edit C: \ Users \ hoge \ testproject \ testproject \ urls.py
as follows.
urls.py
from django.contrib import admin
from django.urls import path
from django.urls import include #add to
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("testapp.testappurls")) #add to
]
Create a file called testappurls.py
under C: \ Users \ hoge \ testproject \ testapp
(it doesn't exist by default). Edit the file as follows:
testappurls.py
from django.urls import path
from . import views
app_name = 'testapp'
urlpatterns = [
path('', views.index, name='index'),
]
Edit C: \ Users \ hoge \ testproject \ testapp \ views.py
as follows.
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse #add to
def index(request):#add to
return HttpResponse('Harowa')#add to
The display of http://127.0.0.1:8000/ should look like this:
Deploy this. We will create some configuration files.
(django-heroku) C:\Users\hoge\testproject>echo web: gunicorn testproject.wsgi --log-file - > Procfile
(django-heroku) C:\Users\hoge\testproject>echo python-3.7.0 > runtime.txt
(django-heroku) C:\Users\hoge\testproject>pip install django-heroku
(django-heroku) C:\Users\hoge\testproject>pip install gunicorn
(django-heroku) C:\Users\hoge\testproject>pip freeze > requirements.txt
Pip
under Anaconda environment is not recommended, but some modules cannot be installed with conda
, so I have to use it.Currently it is a local debug environment, so make it a production environment. Edit the DEBUG
and ʻALLOWED_HOSTS parts of
C: \ Users \ hoge \ testproject \ testproject \ settings.py` as follows.
settings.py
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
Create a git repository and push it to Heroku. It is assumed that you have already installed the git and Heroku command line interface (CLI).
(django-heroku) C:\Users\hoge\testproject>git init
(django-heroku) C:\Users\hoge\testproject>git add .
(django-heroku) C:\Users\hoge\testproject>git commit -m "first commit"
(django-heroku) C:\Users\hoge\testproject>heroku login
(django-heroku) C:\Users\hoge\testproject>heroku create hibit-webpage-test
(django-heroku) C:\Users\hoge\testproject>heroku config:set DISABLE_COLLECTSTATIC=1
(django-heroku) C:\Users\hoge\testproject>git push heroku master
Now it's published!
Go to https://hibit-webpage-test.herokuapp.com/ and you should see something like this:
For the time being, I just aimed to publish Web content (only characters) as a URL, so I'm not thinking about database migration or security. I'm going to write a sequel in response to a lot of strange things.
https://qiita.com/noraricl/items/08937a508a2abecc7179 https://qiita.com/frosty/items/66f5dff8fc723387108c
Recommended Posts