Python3でwebアプリを創りたい!
→ Django!
I made the simplest sample web app. (only 27 lines)
超シンプルなサンプルwebアプリを作りました。(たった27行)
import os webappname = os.path.basename(__file__)[0:-3] if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE', webappname) import django.core.management django.core.management.execute_from_command_line([ '', 'runserver' ]) SECRET_KEY = 'secret' ROOT_URLCONF = webappname WSGI_APPLICATION = webappname + '.application' import django.core.wsgi application = django.core.wsgi.get_wsgi_application() DEBUG = True #ALLOWED_HOSTS = [ "localhost" ] # webapp main from django.http import HttpResponse from django.shortcuts import redirect, render def root(req): return HttpResponse("<h1>simple django</h1>") from django.contrib import admin from django.urls import path urlpatterns = [ path('', root), ]
At the first, install Django.
まずは Django のインストール
pip3 install django
If you want to run after saving that python program as the name 'test.py'
動かすときは、さきほどのPythonプログラムを 'test.py' として保存して
python3 test.py
Try to access 'http://localhost:8000/' on your browesr!
ブラウザで 'http://localhost:8000/' へアクセス!
It's simple isn't it?
シンプルでしょ?
This is a sample program using the parameter and redirecting (see also official document)
パラメータを使ったり、リダイレクトしたりするサンプルはこちら(詳細は公式ドキュメントをどうぞ!)
# webapp main from django.http import HttpResponse from django.shortcuts import redirect, render def root(req): return HttpResponse(""" <h2>hello django</h2> <a href=t1?p=param>param</a><br> <a href=t2>redirect</a><br> """) def test1(req): return HttpResponse(req.get_full_path()) def test2(req): return redirect("https://fukuno.jig.jp/2388") from django.contrib import admin from django.urls import path urlpatterns = [ path('', root), path('t1', test1), path('t2', test2), ]
links
- Django
- Django documentation | Django documentation | Django
- Django ドキュメント | Django documentation | Django
- simpledjango src on GitHub