请求 AJAX url 时 Django 中 "url" 的 NoReverseMatch 错误
NoReverseMatch Error at "url" in Django while requesting AJAX url
我正在尝试刷新我的 Django HTML 页面中的 table 数据,而不是每 10 秒后刷新整个页面......我正在使用 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”的应用程序中创建了一个模型,我正在使用此代码将其数据传递给此 table -
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 部分中
但这给出了同样的错误
项目结构-
任何帮助将不胜感激!!谢谢!!
您在 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>
我正在尝试刷新我的 Django HTML 页面中的 table 数据,而不是每 10 秒后刷新整个页面......我正在使用 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”的应用程序中创建了一个模型,我正在使用此代码将其数据传递给此 table -
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 部分中但这给出了同样的错误
项目结构-
任何帮助将不胜感激!!谢谢!!
您在 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>