Python Web Application Django

Writing your first Django app

建立專案

1
2
3
django-admin startproject mysite

mysite/urls.py

啟動 Server

1
python manage.py runserver

建立 Polls App

1
python manage.py startapp polls

建立一個 views.py

1
2
3
4
5
from django.http import HttpResponse


def index(request):
return HttpResponse("Hello, world. You're at the polls index.")

建立一個 urls.py

1
2
3
4
5
6
7
from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
]

修改 mysite/urls.py
1
2
3
4
5
6
7
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]

在 run 一次就可以看到剛剛寫的 Hello, world. You’re at the polls index.

1
python manage.py runserver

參考