How to use Django on Google App Engine / Python

environment

It is work in the environment where.

Creating a Django app

First, create a project. Here, it is called gaedjango.

If you use django included in the SDK, it will be as follows. For other environments, please modify accordingly.

$ export PYTHONPATH='/usr/local/google_appengine/lib/django-1.5/'
$ /usr/local/google_appengine/lib/django-1.5/django/bin/django-admin.py startproject gaedjangoapp

A project will be created in the gaedjangoapp directory.

Creating an app.yaml file

The Google App Engine application is a file called app.yaml You need to configure the application. Python Application Configuration with app.yaml

app.yaml


application: gaedjangoapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes

libraries:
- name: django
  version: "1.5"

builtins:
- django_wsgi: on

appengine_config.py When using Django of SDK, set PYTHONPATH.

For Django1.5, also set DJANGO_SETTINGS_MODULE.

appengine_config.py


# -*- coding: utf-8 -*-
import os
import sys

if os.environ.get('SERVER_SOFTWARE','').startswith('Dev'):
    sys.path.append('/usr/local/google_appengine/lib/django-1.5/')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gaedjangoapp.settings")

Start development server

Usually in django apps,

$ python manage.py runserver

Start the development server with This time it's Google App Engine, so

$ dev_appserver.py .

Start with.

In this state, if you access [http: // localhost: 8080](http: // localhost: 8080), It worked! And the screen in the initial state is displayed.

Creating a hello app

Create an app that displays Hello World.

$ python manage.py startapp hello

Will create an empty app in the hello directory.

Next, make hello / view.py as follows.

hello/view.py


# -*- coding:utf-8 -*-

from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, world.")

Next, set urls.py.

gaedjangoapp/urls.py


from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    (r'^$', 'gaedjangoapp.hello.views.index'),
)

Finally, add the application to settings.py.

gaedjangoapp.settings.py


INSTALLED_APPS = (
    ~~~~~
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'hello',
)

In this state, if you access [http: // localhost: 8080](http: // localhost: 8080), Hello, world. It will be displayed.

Recommended Posts

How to use Django on Google App Engine / Python
Use ndb.tasklet on Google App Engine
[Python] Run Flask on Google App Engine
How to use Google Assistant on Windows 10
Memorandum on how to use gremlin python
Use external modules on Google App Engine
How to use Python Kivy ④ ~ Execution on Android ~
Tweet (API 1.1) on Google App Engine for Python
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
[Python] [Django] How to use ChoiceField and how to add options
How to build a Django (python) environment on docker
PIL with Python on Windows 8 (for Google App Engine)
Python: How to use async with
[Python] How to use Pandas Series
How to use Dataiku on Windows
How to use Requests (Python Library)
How to use SQLite in Python
Notes on how to use pywinauto
Notes on how to use featuretools
How to use homebrew on Debian
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
Notes on how to use doctest
[Python] How to use Typetalk API
[Hyperledger Iroha] Notes on how to use the Python SDK
Don't lose to Ruby! How to run Python (Django) on Heroku
How to use a library that is not originally included in Google App Engine
[Latest] How to use Python library to save Google image search & use Chrome Driver on ubuntu
Migrate Django applications running on Python 2.7 to Python 3.5
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
[Kivy] How to install Kivy on Windows [Python]
How to update Google Sheets from Python
How to use Google Test in C
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
How to use python put in pyenv on macOS with PyCall
How to erase Python 2.x on Mac.
[Python] Organizing how to use for statements
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to deploy a Django app on heroku in just 5 minutes