Python
Django
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'One.apps.OneConfig', ] ... Time_Zone = 'Asia/Taipei'
在專案資料夾下,編輯 urls.py
from django.contrib import admin from django.urls import path from django.urls import include urlpatterns = [ path('one/', include("One.urls")), path('admin/', admin.site.urls) ]
在應用程式資料夾下,新增檔案 urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.index, name="index"), path('test', views.test, name="test"), path('show', views.show, name="show"), ]
在應用程式資料夾下,新增資料夾 templates
在 templates 資料夾,新增一個 show.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> {% for item in fruits %} <p> {{ item }} </p> {% endfor %} </body> </html>
撰寫 views.py 對應 urls.py 中的設定
from django.shortcuts import render from django.http import HttpResponse # Create your views here. def index(request): return HttpResponse("Hello, My First Web.") def test(request): return HttpResponse("Hello, My First Web. 另外一頁") def show(request): s = ["Apple", "Banana", "Lemon", "Grape"] return render(request, 'show.html', {"fruits": s})
{"fruits": s}
驗收成果
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up