zoukankan      html  css  js  c++  java
  • django将数据库中数据直接传到html

    1、当然,前提是建立和配置好django数据库啦~

    2、python后台函数中引入需要的表

    #要读取home这个APP下的models.py文件,引入其中的Student_message_uneditable和Student_message_editable两张数据表
    from home.models import Student_message_uneditable,Student_message_editable

     

     

    3、python后台函数中查询数据

    # 获取表中的某一条数据,用:表名.objects.get(查询条件(等号左边是列名,右边是具体值))
    student_message_uneditable=Student_message_uneditable.objects.get(student_id=userid)

    在这里,student_message_uneditable即为查询到的那一条数据,用student_message_uneditable.列名 即可获得那一条数据中具体的各个数据的值

     

    4、在python后台函数中的变量值将数据传到html页面
    #向前端页面当前函数中所有的变量传到html中,直接用render_to_response()和locals()
    return render_to_response("student_center.html",locals())

     

    4、html页面中使用数据

    将获取到的数据放入文本框

    <input value="{{ student_message_uneditable.student_name }}">

    使用获取到的数据判断下拉框的取值

    <select name="sex" id="stu_sex" class="form-control">
        <option {%  if student_message_uneditable.student_sex == 1 %}selected{% endif %}></option>
        <option {%  if student_message_uneditable.student_sex == 0 %}selected{% endif %}></option>
    </select>

     

     

  • 相关阅读:
    不使用border-radius,实现一个可复用的高度和宽度都自适应的圆角矩形
    实现一个div在浏览器水平居中
    HTML画布(2)
    《10X工作法制》笔记
    消息体的上传格式
    在rpc链路中的工作总结
    同属姓名注入spring报错
    《事实》听后感
    jdk8使用stream对指定值去重以及其他stream用法
    如何写好业务(待续)
  • 原文地址:https://www.cnblogs.com/zealousness/p/7450495.html
Copyright © 2011-2022 走看看