(This is for local verification only, please do not use it in a production environment)
With some web frameworks such as flask and pyramid, it was possible to create an application with one file. This was useful for a little confirmation.
By the way, I felt that django was tightly coupled and heavy, probably because of the full stack framework. Specifically, I thought that I couldn't confirm a little unless I followed the steps below.
I was wondering why I had to create this project.
After playing around with it, I was able to create a hello world application with one file.
There are some points.
--The dispatch of view is read starting from ROOT_URLCONF in settings.
--Files executed directly in python are loaded as " __ main__ "
modules. This can be referenced with __name__
--Settings can be added directly with settings.configure ().
--execute_from_command_line () allows you to use commands as if you were using manage.py.
You can create a hello world application like this.
# -*- coding:utf-8 -*-
# hello.py
import sys
from django.conf.urls import url, patterns
from django.http import HttpResponse
def index(request):
import random
return HttpResponse('Hello World. random={}\n'.format(random.random()))
urlpatterns = patterns(
"",
url(r'^$', index),
)
if __name__ == "__main__":
from django.core.management import execute_from_command_line
from django.conf import settings
settings.configure(
ALLOWED_HOSTS=["*"],
DEBUG=True,
ROOT_URLCONF=__name__,
MIDDLEWARE_CLASSES=(
'django.middleware.common.CommonMiddleware',
)
)
# using this via 'python server-example.py runserver'
execute_from_command_line(sys.argv)
It feels like running in python normally.
$ python hello.py runserver
Performing system checks...
System check identified no issues (0 silenced).
November 30, 2014 - 08:16:31
Django version 1.7.1, using settings None
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
The result of accessing with curl.
$ curl http://localhost:8000
Hello World. random=0.2760814709302729
$ curl http://localhost:8000
Hello World. random=0.3586777782575409
$ curl http://localhost:8000
Hello World. random=0.08597643541076005
$ curl http://localhost:8000
Hello World. random=0.1478560402662984
Only the necessary settings are added to hello world, so if you want to do other things, you need to add a little more settings.
Recommended Posts