[PYTHON] Create a one-file hello world application with django

(This is for local verification only, please do not use it in a production environment)

Can django be confirmed without creating a project?

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.

  1. Create a project
  2. Update the configuration file
  3. Add relevant parts (views.py, urls.py, etc.)

I was wondering why I had to create this project.

one file hello world in django

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)

Execution method

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

Finally

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

Create a one-file hello world application with django
Create a "Hello World" (HTTP) server with Tornado
Create a homepage with django
Deploy a Django application with Docker
Build a web application with Django
Create a file uploader with Django
I made a WEB application with Django
hello world with ctypes
Create a dashboard for Network devices with Django!
Hello, World with Docker
Hello World on Django
Create a Python console application easily with Click
Hello world with flask
To myself as a Django beginner (3)-Hello world! ---
Create a Todo app with Django REST Framework + Angular
I tried to create a table only with Django
Create a Todo app with the Django REST framework
Create a Todo app with Django ③ Create a task list page
A memo about building a Django (Python) application with Docker
Create a Todo app with Django ⑤ Create a task editing function
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Draw hello world with mod_wsgi
Hello World with Flask + Hamlish
Create an API with Django
Until hello world with zappa
Web application creation with Django
Create a Django login screen
Create a heatmap with pyqtgraph
Create a directory with python
Hello World (beginners) on Django
Python starting with Hello world!
[Learning memo] How to make an application with Django ~ Until Hello World is displayed ~
Create a web application that recognizes numbers with a neural network
Create a web API that can deliver images with Django
Create a Todo app with Django ① Build an environment with Docker
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Create a social integration API for smartphone apps with Django
[Python] Create a screen for HTTP status code 403/404/500 with Django
Measure Django application coverage with Coverage.py
Hello, world! With virtual CAN communication
Create new application use python, django
Django Tips-Create a ranking site with Django-
Twitter posting application made with Django
[Note] Hello world output with python
Hello World! By QPython with Braincrash
Create a poisson stepper with numpy.random
Create a LINE Bot in Django
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Create a temporary file with django as a zip file and return it
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)
Rails users try to create a simple blog engine with Django
Launched a web application on AWS with django and changed jobs
Create a REST API to operate dynamodb with the Django REST Framework
[Python] Web application from 0! Hands-on (2) -Hello World-
Create an application by classifying with Pygame
Hello World and face detection with opencv-python 4.2
Create a Python function decorator with Class
Create RESTful APIs with Django Rest Framework
A simple RSS reader made with Django
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.