美文网首页
Django笔记7:前端页面制作

Django笔记7:前端页面制作

作者: _百草_ | 来源:发表于2021-12-02 09:48 被阅读0次
  1. 控件平台:http://www.htmleaf.com/
  2. APP下新建static文件夹


    image.png

    然后解压资源粘贴负责到static

  3. 修改welcome.html 中js/css等文件中路径
    详见django静态文件访问

4. 新页面添加菜单

方式1:复制粘贴
不推荐
方式2:菜单的html代码做为组件,然后其他页面去调用
比较不错
方式3:菜单作为后台卫衣能返回的html,也就是唯一的render函数内的那个html参数。然后再菜单welcome.html中把其他各个页面都当作一个子页面一个个引入
render使用方法:

def render(request, template_name, context=None, content_type=None, status=None, using=None):
    """
    Return a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    """
    content = loader.render_to_string(template_name, context, request, using=using)
    return HttpResponse(content, content_type, status)
  • request:用于生成响应的请求对象
  • template_name:要使用的模板的完整名称,可选参数
  • context:添加到模板上下文的一个字典。默认是一个空字典。若字典中某个值是可调用的,视图将渲染模板之前调用它
  • content_type:生成文档要使用的MIME类型。默认为DEFAULT_CONTENT_TYPE设置的值。默认为text/html
  • status:响应的状态码。默认200
  • useing:用于加载模板的模板引擎名称

view.py文件
# -*- coding:utf-8 -*-
from django.shortcuts import render
from .models import ClassRecord


# Create your views here.
def record_name(request):
    # 基于函数的视图,即视图函数(还有基于类的视图,与它不同)
    # request是必须的
    records = ClassRecord.objects.all()
    return render(request, "record/name.html", {"records": records})
# render()将数据渲染到指定模板,是render_to_response的快捷方式,自动使用RequestContext
# 第一参数request,必须
# 第2参数,模板位置
# 第3参数,所传送的数据;数据使用类字典的形式传送给模板
# render_to_response(),与之效果相同
# 每个app中都可以有一个专门的模板目录,名称固定为templates
"""
创建目录
├─app_0819
│  │
│  ├─templates 
│  │  │  base.html
│  │  │
│  │  └─record
│  │          name.html

templates默认存放本应用所需模板的目录
若不用自定义方式指定模板位置,Django会在运行时自动来这里查找render()函数中所执行的模板文件

base.html 公共部分,在其他文件中只需编写个性部分的代码
也就是说,在Django的模板文件中,可以有继承功能。
参考:
  1. Django基础之render()

相关文章

网友评论

      本文标题:Django笔记7:前端页面制作

      本文链接:https://www.haomeiwen.com/subject/kdpmvltx.html