我正在嘗試重繪 我的 Django HTML 頁面中的表格資料,而不是每 10 秒后重繪 整個頁面......為此我在 Django 中使用 AJAX
這是我要呈現的HTML 頁面-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>temp1</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>Hello there</h1>
<h1>{{info_data}}</h1>
<table id="_appendHere" class="table table-striped table-condensed">
<tr>
<th>Username</th>
<th>Email</th>
<th>Gender</th>
</tr>
{% for item in info_data %}
<tr><td>{{item.username}} - {{item.email}} - {{item.gender}}</td></tr>
{% endfor %}
</table>
</body>
<script>
var append_increment = 0;
setInterval(function() {
$.ajax({
type: "GET",
url: {% url 'App1:temp' %}, // URL to your view that serves new info
})
}, 10000)
</script>
</html>
我在一個名為“ App1 ”的應用程式中創建了一個模型,我使用此代碼將其資料傳遞給該表 -
from django.shortcuts import render
from App1.models import Info
# Create your views here.
def tempPage(request):
info_data=Info.objects.all()
context={"info_data":info_data}
return render(request,"App1/temp1.html",context)
這是 App1 的 urls.py -
from django.contrib import admin
from django.urls import path,include
from App1 import views
app_name = 'App1'
urlpatterns = [
path('temp/', views.tempPage,name="tempPage"),
]
但我在 URL http://localhost:8000/temp/上收到此錯誤-
NoReverseMatch at /temp/
Reverse for 'temp' not found. 'temp' is not a valid view function or pattern name.
我不確定我要去哪里錯了
我什至為應用程式添加了一個命名空間,并將其包含在 AJAX 請求“App1:temp”的 url 部分中
但這給出了同樣的錯誤
專案結構 -

任何幫助都將不勝感激!!謝謝!!
uj5u.com熱心網友回復:
您的url更改temp中有拼寫錯誤tempPage
<script>
var append_increment = 0;
setInterval(function() {
$.ajax({
type: "GET",
url: {% url 'App1:tempPage' %}, // URL to your view that serves new info
})
}, 10000)
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422720.html
標籤:
上一篇:Django-errorOSError:dlopen()failedtoloadalibrary:cairo/cairo-2/cairo-gobject-2
