weixin_33739523 2013-03-10 22:40 采纳率: 0%
浏览 39

Django和Ajax

Im trying to get a basic app working in Django that incorporates AJAX. The app will take a domain name and will then send it to the server, which will do a dns lookup on it and then send the response back via AJAX to the client.

Views

from django.http import *
from django.shortcuts import render_to_response
from django.template import RequestContext

import sys
import os
import socket

def main(request):
    if request.method == 'POST':
       dig_input = request.POST['digInput']
       digoutput = socket.gethostbyname(dig_input)
       return render_to_response('digajax.html', {'response': digoutput}, context_instance=RequestContext(request))
    else:
       return render_to_response('digajax.html', context_instance=RequestContext(request))

URLs

url(r'^digajax$', 'digajax.views.main'),

Templates

<!DOCTYPE html>
<html lang="en">
 <head>
    <meta charset="utf-8">
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>

<script type="text/javascript">
function send_request(){
  $.get(location.href, function(data){
    $("#output").html(data.output);
  });
}

 </head>
  <body>
        <form method="post" name="diginput form" action="/digajax">
          {% csrf_token %}
                                <input name="digInput" id="digInput" type="text">
                                <input type="button" onclick="send_request();" value="Request this page with AJAX">lookup</input>
                </form>
{% if response %}
  <div id="output">
    <p>{{ response|linebreaksbr }}</p>
  </div>
{% else %}
  <p>no</p>
{% endif %}

  </body}
</html>

Without AJAX everything is working. Now that I want to use AJAX Im not what what code I should add to each section.

Any help would be really appreciated...............

  • 写回答

1条回答 默认 最新

  • weixin_33724659 2013-03-10 23:20
    关注
    1. Django provides an method on the request object your view is passed which will tell you whether the request was made via XmlHttp, request.is_ajax().
    2. If that method returns true, you probably want to return only the fragment of the page you want to update, instead of the whole page.
    3. If that method returns false, you probably want to return the entire page, since the user either has JavaScript turned off, or there was some type of error which caused the view to be requested normally.

    So, your view should look like:

    def main(request):
        if request.method == 'POST':
           dig_input = request.POST['digInput']
           digoutput = socket.gethostbyname(dig_input)
           if request.is_ajax():
                return HttpResponse("<p>%s</p>" % digoutput)
           else:
                return render(request, 'digajax.html', {
                    'response': digoutput
                })
        else:
           return render(request, 'digajax.html')
    

    Your JavaScript code should be look like:

    <script type="text/javascript">
    function send_request(){
      $.get(location.href, function(data){
        $("#output").html(data);
      });
    }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog