Django ajax no refresh:django view without redirecting or refreshing a page

后端 未结 3 487
攒了一身酷
攒了一身酷 2021-01-17 00:29

I\'d like to execute some Python code if that button is pressed on web.The thing is, I need the page not to redirect or refresh or anything.

相关标签:
3条回答
  • 2021-01-17 01:17

    there's a lot of info on internet about django and ajax, just google it.

    http://www.youtube.com/watch?v=lllVAFbRGfI

    or as Mingyu said use dajax

    0 讨论(0)
  • 2021-01-17 01:19

    Just use jQuery ajax,it is easy to do

    in django views.py

    def fun1(request):
        user_input = request.GET.get('value')
        #put your code here
        return HttpResponse('what you want to output to web')
    

    urls.py

    url(r'^link-to-fun1$', views.fun1),
    

    html

    <html>
    <head>
    <title>Your title</title>
    <script type="text/javascript" src="/media/js/jquery-1.10.2.min.js"></script>
    <script>
    function myFun() {
        $.get('link-to-fun1/',{"value":"get_the_value_from_web"},function(ret) {
                    return ret;//you can handle with return value ret here
                });
    }
    </script>
    </head>
    <body>
    

    More see jQuery $.get method

    0 讨论(0)
  • 2021-01-17 01:32

    You may want to try the Ajax library for Django project:

    http://www.dajaxproject.com/

    In case you've never heard of Ajax, here's the definition from Wikipedia:

    Ajax (an acronym for asynchronous JavaScript and XML) is a group of interrelated web development techniques used on the client-side to create asynchronous web applications.

    With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page.

    0 讨论(0)
提交回复
热议问题