【django】【基础】templates

templates

  • 返回模板文件:
    • 创建templates文件夹:mkdir supporter/templates
    • 创建index.html文件:vim supporter/templates/index.html
    • body中添加测试内容:<h1>hello django</h1>
    • 修改项目设置:
      TEMPLATES = [
          {
              'BACKEND': 'django.template.backends.django.DjangoTemplates',
              'DIRS': [
                  os.path.join(BASE_DIR, 'supporter/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',
                  ],
              },
          },
      
    • 修改supporter/views.index方法:return render(request=request, template_name='index.html')
    • 访问:http::127.0.0.1:8000/supporter/
  • 数据渲染:
    • 返回模板文件时添加:return render(request=request, template_name='index.html', context={'hello': 'hello django!!!'})
    • 模板引擎中渲染:<h1>{{ hello }}</h1>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容