我在 SO 上找到了很多答案,但我對所有這些的理解仍然太新,無法理解如何實施,所以只需發布我的確切問題:
我有一個表格,它是我的資料庫的過濾視圖,顯示在我的應用程式的一頁上。它有一個注釋列,其中每個單元格都使用 content-id-editable。我想添加一個保存按鈕,當有人希望保存該特定筆記部分單元格時,我希望他們能夠按“保存”并將更新的單元格發布到資料庫中。
這是我的views.py:
def notes(request):
try:
if request.method == 'POST':
note = request.POST['tm.Notes']
updated_note = TM.Object.save(Notes__=note)
return render(request,'homepage/detail.html', {'Notes': updated_note})
else:
return render(request,'homepage/detail.html', {})
except TM.DoesNotExist:
raise Http404("Info Does Not Exist")
這是我的 html:
<table class= "content-table">
<thead>
<tr>
<th>Name</th>
<th>Distributor</th>
<th>State</th>
<th>Brand</th>
<th>Cell</th>
<th>Email</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
{% for tm in territory_manager %}
<td style="white-space:nowrap;">{{ tm.Name }}</td>
<td style="white-space:nowrap;">{{ tm.Distributor }}</td>
<td style="white-space:nowrap;">{{ tm.State }}</td>
<td style="white-space:nowrap;">{{ tm.Brand }}</td>
<td style="white-space:nowrap;">{{ tm.Cell }}</td>
<td style="white-space:nowrap;">{{ tm.Email }}</td>
<td style="white-space:wrap;" input type="submit" class="primary" value="Submit" ><div contenteditable>{{ tm.Notes }}</div></td>
</tr>
{% endfor %}
</tr>
{% else %}
<h2>No Results for: {{ state }} </h2>
{% endif %}
</tbody>
</table>
我知道這是非常錯誤的,似乎應該有一個非常簡單的解決方案,但是我越是嘗試從 SO 中應用某些東西,我就越困惑。任何幫助表示贊賞。謝謝!
uj5u.com熱心網友回復:
我不建議這樣設定,因為每次有人保存筆記時頁面都會重繪 ......
為什么不通過 AJAX 發送 POST 資料?
您需要撰寫一個可以在單擊按鈕時呼叫的 JavaScript 函式。
你可以做一些簡單的事情,比如......
筆記.js
function saveNoteAjax() {
return $.ajax({
type: 'POST',
url: 'your route to the function notes function in views.py',
data: {note: document.getElementById('yourNotesInput_ID').value}
})
}
async function SaveLoadNote(){
await saveNoteAjax()
*use another Ajax call to get the new notes from the DB*
*insert some function to refresh the notes on-screen*
}
視圖.py
def notes(request):
if request.is_ajax():
note = request.POST['note']
*insert your code for updating your DB table*
return HttpResponse('')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/340998.html
