创建投票首页
from django.conf.urls import url,include
from django.contrib import admin
from . import views #.是当前目录的意思(和urls.py同级,也可以from polls import views)
urlpatterns = [
url(r^$, views.index,name=index), #匹配到空白,都用views.index函数进行响应,那么为函数名字
]
return render(request, index.html)
TEMPLATES = [
{
BACKEND: django.template.backends.django.DjangoTemplates,
DIRS: [os.path.join(BASE_DIR, templates)], #templates模板的位置,项目目录同级
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
编写问题页面
urlpatterns = [
url(r^$, views.index,name=index),
url(r(d+)/$, views.a,name=a),
#d+为匹配数字(+为至少匹配到一个数字)
# ()为传参(把匹配到的数字作为视图函数a的参数 )
]
from django.shortcuts import render
def index(request):
return render(request,index.html)
def a(request,id): #在urls.py中匹配到的参数的值用变量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文件中接受的参数,用{{}}表示
div>
body>
html>
编写结果页面
urlpatterns = [
url(r^$, views.index,name=index), #r‘’单引号里面什么也不填写那么就是匹配空串(在任何字符之间都有空串),不管在ip/polls/后面填写什么都将匹配首页
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、编写result.html
"UTF-8">
Title
{{id}}result
end