I am trying to refresh the table data in my Django HTML page, without refreshing the whole page after every 10 seconds ... for which I am using AJAX in Django
This is the HTML Page I want to render -
<!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" >
<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>
I have created a model inside an app called "App1" whose data I am passing to this table using this code -
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)
This is the urls.py for App1 -
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"),
]
But I get this error on the URL http://localhost:8000/temp/ -
NoReverseMatch at /temp/
Reverse for 'temp' not found. 'temp' is not a valid view function or pattern name.
I am not sure where I'm going wrong
I even added a namespace for the App and included that in the url part of the AJAX request "App1:temp"
But that gives the same error
Project Structure -
Any help would be highly appreciable!! Thanks!!
CodePudding user response:
You have typo error in your url change temp to 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>

