1. 程式人生 > >django將數據庫中數據直接傳到html

django將數據庫中數據直接傳到html

ssa lec enter endif return 配置 turn 文本 所有

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>

django將數據庫中數據直接傳到html