You want to use AJAX + DJANGO to make sure that the value will continue to change automatically without having to manually refresh the screen.
In this case, I don't know how to write the code, so I'm asking you a question.
templates/sign/index.html
<html>
<meta charset="UTF-8">
<body>
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<div id="Context">This is original</div>
<script type="text/javascript">
function ajaxTest(){
$.ajax({
type : "GET",
url : "sign/",
dataType : "text",
error : function() {
alert('Communication Failure!!');
},
success : function(data) {
$('#Context').html(data);
}
});
}
playAlert = setInterval(function() {
ajaxTest();
});
</script>
</body>
</html>
views.py
import os
from django.shortcuts import render
from django.shortcuts import HttpResponse
def AjaxRespon(request):
#returnHttpResponse ('<divid = "Context">Test</div>')
key = open(os.path.join(os.path.dirname(__file__), 'data'), mode='r', encoding='utf-8').read()
context = {'key':'<div id = "Context">{}</div>'.format(key)}
return render(request, 'sign/index.html', context)
urls.py
from . import views
from django.urls import path
urlpatterns = [
path('sign/', views.AjaxRespon, name='index')
]
I'm going crazy. If I use HttpResponse on views.py, it works normally, but it doesn't move dynamically, but I can't use it because it just becomes a static page when I use a render...
python django database
According to the code, AjaxRespon(request)
is actually a simple text/html
. Try something like this.
def AjaxRespon(request):
# an intermediate omission
return '<div id = "Context">{}</div>'.format(key)
582 PHP ssh2_scp_send fails to send files as intended
573 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
620 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.