創(chuàng)建投票首頁(yè)
from django.conf.urls import url,include
from django.contrib import admin
from . import views #.是當(dāng)前目錄的意思(和urls.py同級(jí),也可以from polls import views)
urlpatterns = [
url(r^$, views.index,name=index), #匹配到空白,都用views.index函數(shù)進(jìn)行響應(yīng),那么為函數(shù)名字
]
return render(request, index.html)
TEMPLATES = [
{
BACKEND: django.template.backends.django.DjangoTemplates,
DIRS: [os.path.join(BASE_DIR, templates)], #templates模板的位置,項(xiàng)目目錄同級(jí)
APP_DIRS: True,
OPTIONS: {
context_processors: [
django.template.context_processors.debug,
django.template.context_processors.request,
django.contrib.auth.context_processors.auth,
django.contrib.messages.context_processors.messages,
],
index.html
"en">
"UTF-8">
polls
"container">
polls
編寫(xiě)問(wèn)題頁(yè)面
urlpatterns = [
url(r^$, views.index,name=index),
url(r(d+)/$, views.a,name=a),
#d+為匹配數(shù)字(+為至少匹配到一個(gè)數(shù)字)
# ()為傳參(把匹配到的數(shù)字作為視圖函數(shù)a的參數(shù) )
]
from django.shortcuts import render
def index(request):
return render(request,index.html)
def a(request,id): #在urls.py中匹配到的參數(shù)的值用變量id接受
return render(request,a.html,{id:id})
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div class="container">
<h1>{{id}}questionh1> #在views.py文件中接受的參數(shù),用{{}}表示
div>
body>
html>
編寫(xiě)結(jié)果頁(yè)面
urlpatterns = [
url(r^$, views.index,name=index), #r‘’單引號(hào)里面什么也不填寫(xiě)那么就是匹配空串(在任何字符之間都有空串),不管在ip/polls/后面填寫(xiě)什么都將匹配首頁(yè)
url(r(d+)/$, views.a,name=a),
url(r(d+)/result/$, views.result,name=result)
]
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,index.html)
def a(request,id):
return render(request,a.html,{id:id})
def result(request,id):
return render(request,result.html,{id:id})
3、編寫(xiě)result.html
"UTF-8">
Title
{{id}}result
end