django+js+ajax implements the method of refreshing the page

  • 2020-06-01 10:11:46
  • OfStack

This article illustrates an example of how django+js+ajax can refresh a page. I will share it with you for your reference as follows:

During the server development, in order to make it convenient to open an interface to the server for operation, django can be used to make web pages and operate the server through the pages. This allows the maintenance of the server to be done in a more user-friendly manner, rather than through SecureCRT. It also provides planning and maintenance personnel with the ability to handle routine tasks.

Here's a very small point:

How does js make a request
How does django respond to requests
How does js receive the response to refresh the page

js part

Let's define an button in html and set id to btnTerminalSvr. Define a static title ntfText for 1 feedback


<h2 id="ntfText" > ready </h2></br>
<button type="button" class="btn btn-success" id="btnTerminalSvr"> Shut down the server </button>

Write js to respond to this button event


<script type="text/javascript">
 $('#btnTerminalSvr').on('click', function () {
    alert(' Hey, I heard you clicked the button ...' );
    ...
  });
</script>

Perfect the request call for one ajax request and the processing after the callback:


<script type="text/javascript">
 $("#btnTerminalSvr").click(function(){
     $.ajax({
        url: './terminal_svr',
        type: 'POST',
        data: {},
        dataType: 'json',
        timeout: 10000,
        success: function(result) {
        if ( result.result == "post_success" ) {
          $("#ntfText").html(" A successful ");
        }else {
          $("#ntfText").html(" Repeat initiated ");
        }
        }
       });
  });
</script>

Here we can put some data structures in data in json format, so that we can provide some messages to the server to make the call. When the call succeeds, it will call back function(result). This is the asynchronous common js notation, and when the server makes a response, we can directly modify the html string in the id=ntfText part through the jQuery framework.

django part

Step 1: let's define the corresponding mapping of url for django


url(r'^terminal_svr', views.terminal_svr,name='terminal_svr'),

Step 2: implement the handler in views.py


from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def terminal_svr(request):
  #  So this is using django Own login verification system 
  if not request.user.is_authenticated():
    return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/admin/'))
  doSomething to terminal svr
  a = {}
  a["result"] = "post_success"
  return HttpResponse(json.dumps(a), content_type='application/json')

This one is relatively simple. We can read the information sent by json in request, and judge whether the operation is successful by returning one string to the front segment of json string after the operation is successful.

More about Python related topics: interested readers to view this site "Python introduction and advanced tutorial", "Python coding skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using skills summary", "Python string skills summary" and "Python file and directory skills summary"

I hope this article is helpful to you Python programming.


Related articles: