# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config' # 注册项目
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
from django.urls import path
from app01 import views
urlpatterns = [
path('index/', views.index),
]
from django.shortcuts import render, HttpResponse, redirect
def index(request):
return render(request, "index.html")
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
body>
html>
views.py 中:
from django.shortcuts import render, HttpResponse, redirect
def index(request):
name = '张三'
return render(request, "index.html", {'name': name})
index.html 中:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
<p> 姓名:{{ name }}p>
body>
html>
views.py 中:
from django.shortcuts import render, HttpResponse, redirect
def index(request):
roles = ['超级管理员', '管理员', '一般用户']
return render(request, "index.html", {'roles': roles})
index.html 中:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
<p> {{ roles }}p>
<p> {{ roles.0 }}p>
<p> {{ roles.1 }}p>
<p> {{ roles.2 }}p>
body>
html>
views.py 中:
from django.shortcuts import render, HttpResponse, redirect
def index(request):
user_info = {"name": '张三', 'age': 18}
return render(request, "index.html", {'user_info': user_info})
index.html 中:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
<p> 字典信息:{{ user_info }}p>
<p> 姓名:{{ user_info.name }}p>
<p> 年龄:{{ user_info.age }}p>
body>
html>
views.py 中:
from django.shortcuts import render, HttpResponse, redirect
def index(request):
user_info = [
{"name": '张三', "age": 18},
{"name": '李四', "age": 19},
{"name": '王五', "age": 20}
]
return render(request, "index.html", {'user_info': user_info})
index.html 中:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
<p> 嵌套信息:{{ user_info }}p>
<p> 第一行:{{ user_info.0 }}p>
<p> 第一行中的姓名:{{ user_info.0.name }}p>
body>
html>
views.py 中:
from django.shortcuts import render, HttpResponse, redirect
def index(request):
name = '张三'
return render(request, "index.html", {'name': name})
index.html 中:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
{% if name == '张三' %}
<p> 这个人是张三p>
{% elif name == '李四' %}
<p> 这个人是李四p>
{% else %}
<p>找不到此人p>
{% endif %}
body>
html>
views.py 中:
from django.shortcuts import render, HttpResponse, redirect
def index(request):
user_info = {"name": '张三', 'age': 18}
return render(request, "index.html", {'user_info': user_info})
index.html 中:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1> 模板语法的学习 h1>
{% for item in user_info.items %}
<div>{{ item }}div>
{% endfor %}
{% for key, value in user_info.items %}
<p>{{ key }}p>
<p>{{ value }}p>
{% endfor %}
body>
html>