【发布时间】:2021-04-16 06:46:49
【问题描述】:
我按照 MariaDB 文档实现了以下连接器。 Documentation
这是我的 db.py:
#!/usr/bin/python
import mariadb
conn = mariadb.connect(
user="user",
password="",
host="localhost",
database="db")
cur = conn.cursor()
def get_some_data():
cur.execute("SELECT some_id FROM `db`.some_data")
sid = []
for some_id in cur:
sid.append(some_id)
return sid
conn.close()
到目前为止一切都清楚了,我不清楚的是...如何在 html 中选择一个列表,其中包含从查询中获得的值到我的 django 网页上的数据库?
我应该在views.py 中创建一个函数并让它返回类似这样的内容吗?:
from db import get_some_data
def retrieve_from_db(request):
#some code here
return render(request, 'index.html', context)
我需要context吗?
假设我的index.html如下,我怎么能从模板中调用retrieve_from_db(request)并让这个函数调用get_some_data()并在网络上显示这个信息?
index.html
{% extends "../base.html" %}
{% load static %}
{% load bootstrap4 %}
{% block content %}
{% bootstrap_javascript %}
//Some form to retrieve the data from the .py scripts
<form id="retrieve_db" action="someaction?" method="post">
<div class="form-group">
{% someforloop? %}
<select name="myIdList" id="myIdList">
<option value="{{ some_id }}">{{ some_id }}</option>
</select>
{% endforloop %}
</div>
</form>
{% endblock %}
假设我希望将值显示在列表中:myIdList
我想我分别理解了这些概念,但我需要知道如何连接拼图的各个部分。
如果有人可以帮助我了解如何实施整个周期的“骨架”,那将是非常有帮助的。
【问题讨论】:
标签: python django django-views django-templates