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>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM