如何使用Django和Jquery实现ajax?

时间:2022-12-30 14:30:36

urls.py:

urls.py:

....
(r'^blog/post/$', post),
....

View code:

查看代码:

def post(request):

    if request.method == 'POST':



      post, created = Post.objects.get_or_create(
          title=request.POST.get("title"),
          text=request.POST.get("text"),

      )

    post_titles = [post.title for post in Post.objects.all()]
    return render_to_response("index.html", {"post_titles":post_titles})

Templates:

模板:

<script type="text/javascript" charset="utf-8">
            $(document).ready(function() {

                $("input#submit").click(function() {


                var title = $("#title").val();
                    var text = $("#text").val();    

                    var data = {"title":title,"text":text};

                    $.post("/blog/post/", data, function(data){
                        alert("Data Loaded: " + data);
                    });
                });


            });
        </script>
        .........
        <div id="postbox">
      <form method="post" action="">
            <p>Enter Title</p><input type="text" name="title" id="title">
            <p>Enter Text</p><input type="text" name="text" id="text">
            <input type="submit" id="submit">
          </form>
        </div>

I want the post to get added into the database without page refresh and also "title" and "text" get appended somewhere in the html page. Please tell me what I am doing wrong. Thanks.

我希望在没有页面刷新的情况下将帖子添加到数据库中,并且还在html页面中的某处附加“title”和“text”。请告诉我我做错了什么。谢谢。

1 个解决方案

#1


0  

I have had very good luck using Dajax/Dajaxice, which is well-documented, has several working demos (with code) of examples of integrating Django with ajax through jQuery. I'd give a longer answer if you gave debugging output (e.g., does the alert box come up; what's the text in it). My guess is your view function with 'render_to_response' of 'index.html' is not what you want with ajax. Typically you just want to send some sort of structured data (usually json or xml) with your ajax from the server (to a previously rendered page).

我有很好的运气使用Dajax / Dajaxice,这是有充分记录的,有几个工作演示(带代码)的示例通过jQuery集成Django和ajax。如果给出调试输出,我会给出更长的答案(例如,警报框是否出现;其中的文本是什么)。我的猜测是你的视图函数'index.html'的'render_to_response'不是你想要的ajax。通常,您只想从服务器(到先前呈现的页面)使用您的ajax发送某种结构化数据(通常是json或xml)。

#1


0  

I have had very good luck using Dajax/Dajaxice, which is well-documented, has several working demos (with code) of examples of integrating Django with ajax through jQuery. I'd give a longer answer if you gave debugging output (e.g., does the alert box come up; what's the text in it). My guess is your view function with 'render_to_response' of 'index.html' is not what you want with ajax. Typically you just want to send some sort of structured data (usually json or xml) with your ajax from the server (to a previously rendered page).

我有很好的运气使用Dajax / Dajaxice,这是有充分记录的,有几个工作演示(带代码)的示例通过jQuery集成Django和ajax。如果给出调试输出,我会给出更长的答案(例如,警报框是否出现;其中的文本是什么)。我的猜测是你的视图函数'index.html'的'render_to_response'不是你想要的ajax。通常,您只想从服务器(到先前呈现的页面)使用您的ajax发送某种结构化数据(通常是json或xml)。