好的,所以我一直在嘗試解決這個問題大約一個星期,但我在互聯網上找不到直接的答案:
我正在制作 Django 網站,我需要在 jQuery 中訪問我的資料庫(即 SQLite3)來編輯我的網站內容。
假設我有一個這樣的一維資料庫:

在我的 Django“Views.py”中,我有這個:
from exampleapp.models import database
def page(request):
data = database.objects.all()
return(render(request, "homepage.html", {"data": data}))
在我的 HTML 檔案中,我有這個(這是資料庫的迭代):
{% for x in data %}
<p id = {{x}} > {{x}} </p>
{% endfor %}
在 HTML 頁面上輸出:

無論如何,每個 DOM 元素的 id 都等于它的內容(“apple”、“cheese”等)。我想在 jQuery 中參考這些 id,以便我可以編輯它們。例如,如果想參考 id = "cheese" 的元素,我該如何在 jQuery 中做到這一點?我知道 jQuery 中有一個 Ajax 函式可以決議 JSON。我可以在我的“Views.py”檔案中將我的資料庫轉換為 JSON 格式(但我仍然不知道如何訪問我的資料的這個新的序列化版本,除非我使用 Python 將其保存到 .json 檔案) :
from exampleapp.models import database
from django.core.serializers import serialize
def page(request):
data = database.objects.all()
dataj = serialize("json", data, fields=("title"))
return(render(request, "homepage.html", {"data": data}))
因此在 jQuery 中使用它來獲取 JSON:
$.ajax({
dataType: "json",
url: url, //If I use this method, where do I draw the JSON from? The database file itself?
//Do I have to create a JSON file in my Django "Views.py" of my serialized data and then use that
//path as the URL? This is where I am mixed up.
data: "text",
success: success
})
我看到我可以參考資料庫的唯一方法是將它匯入到 jQuery 檔案中,然后我可以迭代內容以便能夠在 HTML 中獲取資料庫迭代的索引(或者可能忘記 for 回圈HTML 并完全在 jQuery 中使用資料庫)。 如何參考 HTML for 回圈中的專案?
另外(也許是解決我的問題的更有效方法),如何將我的 SQLite3 資料庫轉換為 JSON,以便可以在我的 jQuery 檔案中使用它?或者有沒有更好的方法將我的資料庫“匯入”到我的 Django 內置的 jQuery 檔案中?
uj5u.com熱心網友回復:
好的,所以我終于找到了一個簡單的解決方法!
它涉及3個步驟:
- 在您的“views.py”檔案中:
database = "apple, cheese, oatmeal, nootnoot, sosbrigade"
def homepage(request):
data = database.objects.all() #1. Retrieve Your Database.
datacount = data.count() #2. Get the Number of Items in your Database.
r = [] #3. An empty list to contain the range of numbers from to datacount.
for x in range(0, datacount):
#4. Add the range of numbers to the list r.
r.append(x)
#5. "Zip" both the range numbers (r) and the database items (data) together.
#This will create a list in this format: [(0, item1), (1, item2)...]
d = zip(r, data)
#Return the list d as "data" for the HTML template.
return(render(request, "homepage.html", {"data": d}))
- 在您的 HTML 檔案中:
<!--
{{x.0}} outputs the item **index**
{{x.1}} outputs the **actual** item contents.
when you set the element's id to "data{{x.0}}", it's id will be "data0, data1,
data2... etc", whilst the text of the element is {{x.1}}, which is the database.
-->
{% for x in data %}
<p id = data{{x.0}} > {{x.1}} </p>
{% endfor %}
<!--
This will output:
apple (but it's id will be "data0")
cheese (but it's id will be "data1")
oatmeal (but it's id will be "data2")
nootnoot (but it's id will be "data3")
sosbrigade (but it's id will be "data4")
-->
這解決了我的問題,因為在我的 jQuery 檔案中,我可以系統地參考這個資料庫,而不是通過它們的內容是什么:
- 在您的 jQuery(或 Javascript 檔案;相應地翻譯代碼)中:
$(document).ready(function() {
//I can access the database like this (by index) or I could tweak the
//element's id's in a way that makes it easier for me to access them, since
// I have both an index and the contents.
//I think you can also use the $(":contains("text") function of jQuery to perhaps
//Find the id of the element by it's database content.
$("#data0").text("YASS!")
//This changes "apple" to "YASS!" :P.
});
它非常簡單,但它回答了我的問題并讓我避免了任何 API 呼叫、自定義模板標簽等......
uj5u.com熱心網友回復:
這是一個很大的話題。做你想做的事的行業標準是創建一個 RESTful API。這里最大的問題是 JavaScript(如 jQuery)運行在客戶端瀏覽器上,而不是您控制的服務器上。這會引發各種安全問題。
幸運的是,Django REST Framework 可以提供幫助。
我建議通過 DRF 教程開始。
https://www.django-rest-framework.org/tutorial/quickstart/
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/418697.html
標籤:
上一篇:插入觸發器之前包含錯誤的資料
