视图函数必须至少提供一个参数,代表request 视图函数必须返回HttpResponse对象
HttpResponse:用来响应一个简单的字符串或者流
render函数:主要用于跳转到模板页面 from django.shortcuts import render
render(request, template_name, context=None, content_type= None, status=None) ''' request:每一个视图函数都使用 template_name:模板的名称,html页面的名字 context:向模板传递的数据,格式是一个字典 content_type:响应的类型 默认值是text/html;charset=utf-8 status:响应的状态码,默认是200 ''' redirect:跳转到一个路由地址中from django.shortcuts import redirect
redirect(to, *args, permanent=False) ''' to:跳转到某一个请求地址(路由)中 permanent:默认是False,代表临时重定向,状态码是302 ''' -FileResponse:专门用于文件下载的响应JsonResponse:返回json格式数据的响应对象在项目的根下,新建一个templates 目录 右键templates --> make direstory as --> template folder(非必须) 在settings.py文件中,进行模板的配置
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', ], }, }, ]