###配置文件
##模板系统
1.模板系统指的是 templates。 templates 存放的是 html页面。
2.静态页面(html,css js ) 不和后台交互的。
3.动态页面 和后台进行交互,获取数据放到页面上。
4.作用: 将后台数据返回给 页面。(替换页面中的数据)
5.解析变量的语法 : {{变量名}}
6.单行注释 {##} 快捷键 ctrl + /
7.多行注释 {% comment %}{% endcomment %} 快捷键 shift + ctrl + /
8.传递字典类型的变量,获取其中的值,需要使用 变量名.key的方式
###views.py文件
from django.shortcuts import HttpResponse,render
def hello(request):
'''
名称:命名任意,但是需要遵守命名规则和见名知意.
:param request: 至少要有一个参数,一般命名为ruquest,因为浏览器发送请求的时候
会将所有的内容封装到了django.http的HttpRequest对象中,传入视图函数的第一个参数
因此命名为request.
:return: 向浏览器返回内容,注意有请求必须有响应.
HttpResponse 对象:可以向浏览器返回一段文本信息.
'''
print(request.method)
return HttpResponse('你好啊世界')
def index(request):
userinfo={'name':'张三','age':18}
return render(request,'index.html',{'userinfo':userinfo,'msg':'首页'})
def index2(request):
names=['狄仁杰','安其拉','程咬金']
print(names)
return render(request,'index.html',{'names':names})
class Person():
def __init__(self,name):
self.name=name
self.age=18
def dream(self):
return '{0} dream is play game (age is {1})'.format(self.name,self.age)
def index3(request):
person=Person('张三')
return render(request,'index.html',{'person':person})
def index4(request):
name='zs'
age=18
hobby='game'
print(locals())
# {'hobby': 'game', 'name': 'zs', 'age': 18, 'request': <WSGIRequest: GET '/index4/'>}
return render(request,'index.html',locals())
def index5(request):
userinfo=[
{'name':'zs','age':18},
{'name':'ls','age':8}
]
return render(request,'index.html',{'user':userinfo})
###urls.py
"""dj01 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from dj01 import views
#1.导入HttpResponse类
from django.shortcuts import HttpResponse
#2.创建视图函数
# def hello(request):
# '''
# 名称:命名任意,但是需要遵守命名规则和见名知意.
# :param request: 至少要有一个参数,一般命名为ruquest,因为浏览器发送请求的时候
# 会将所有的内容封装到了django.http的HttpRequest对象中,传入视图函数的第一个参数
# 因此命名为request.
# :return: 向浏览器返回内容,注意有请求必须有响应.
# HttpResponse 对象:可以向浏览器返回一段文本信息.
# '''
# print(request.method)
# return HttpResponse('你好啊世界')
#3.设置路由
urlpatterns = [
path('admin/', admin.site.urls),
path('hello1/',views.hello),
path('index/',views.index),
path('index2/',views.index2),
path('index3/',views.index3),
path('index4/',views.index4),
path('index5/',views.index5),
]
###templates下的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>{{ msg }}</h1>
<hr>
姓名:{{ userinfo.name }},年龄:{{ userinfo.age }}
<hr>
获取列表内容:{{ names.0 }}-->{{ names.1 }}-->{{ names.2 }}
<hr>
获取对象无参函数返回值:{{ person.dream }}
<hr>
获取local() 内置函数:{{ name }},{{ age }},{{ hobby }}
<hr>
获取列表里边的字典内容:张三的名字:{{ user.0.name }} 李四的年龄:{{ user.1.age}}
</body>
</html>