For the time being, the one who creates a homepage with Django at the speed of a second and publishes it on Heroku (Windows compatible)

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.

Virtual environment construction

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 project creation

(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.

image.png

Display only characters

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:

image.png

Deploy

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

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:

image.png

important point

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.

Page that was taken care of

https://qiita.com/noraricl/items/08937a508a2abecc7179 https://qiita.com/frosty/items/66f5dff8fc723387108c

Recommended Posts

For the time being, the one who creates a homepage with Django at the speed of a second and publishes it on Heroku (Windows compatible)
Collect cat images at the speed of a second and aim for the Cat Hills tribe
Run yolov4 "for the time being" on windows
Until you can install blender and run it with python for the time being
I tried running PIFuHD on Windows for the time being
Image crawling summary performed at the speed of a second
Until the start of the django tutorial with pycharm on Windows
The story of making a tool that runs on Mac and Windows at the game development site
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
I measured the speed of list comprehension, for and while with python2.7.
The second night of the loop with for
The advantages and disadvantages of Django that people with one year of experience think
A record of the time it took to deploy mysql on Cloud9 + Rails
Define a division value in Django and easily reflect it on the screen
Turn multiple lists with a for statement at the same time in Python