【问题标题】:NoReverseMatch Error at "url" in Django while requesting AJAX url请求 AJAX url 时,Django 中的“url”出现 NoReverseMatch 错误
【发布时间】:2022-01-28 14:04:57
【问题描述】:

我正在尝试刷新我的 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 部分中

但这给出了同样的错误

项目结构-

任何帮助都将不胜感激!!谢谢!!

【问题讨论】:

  • 在 ajax 中更改 url: {% url 'App1:temp' %},到 url: {% url 'App1:tempPage' %}

标签: python django ajax


【解决方案1】:

您的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>

【讨论】:

    猜你喜欢
    • 2017-03-14
    • 2021-03-07
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2023-03-29
    • 2011-09-29
    相关资源
    最近更新 更多