简体   繁体   English

如何在Django应用中更改DIV内容

[英]How to change DIV contents in django app

I want to replace the DIV contents with the new contents through ajax GET method. 我想通过ajax GET方法用新内容替换DIV内容。 The problem is the whole page is loading into the DIV. 问题在于整个页面正在加载到DIV中。 But i want to load only new contents. 但是我只想加载新内容。

    function demoButton(){

        var forData = $('#For').text();
        var ageData = $('#Age').text();
        var occasionData = $('#Occasion').text();
        var URL ="http://127.0.0.1:8000/result/?age="+ ageData +"&occasion="+ occasionData +"&relationship="+ forData;

$.ajax({
    type: "GET",
    url: URL,
    success: function (response) {
        $("#testDIV").html(response);
    }
}); 
}

And my DIV block is : 我的DIV块是:

<div id="testDIV"> 
 <ul id="products">
 {% for deal in deals %}
  <li data-price="{{ deal.price }}" > 
   <div class="product" >
          <div class="inner"> 
           <a href="/dailydeals/{{ deal.id }}/"><img src="{{ STATIC_URL }}/images/cimages/{{ deal.image }}" alt="image" style='width:177px;height:175px;' /> </a>
            <div class="similarProd"><a href="#">View similar product</a></div>
            <div class="heart"><a href="/usersl/{{user.id}}/{{ deal.id }}"><span>Engraved plaque fsor your<br>

              girlfriend</span></a></div>

            <div class="quickView"><a href="#qv_form{{ deal.id }}" id="fb_pop">QUICK VIEW</a></div>
            <div class="socialMedia"><a href="#"> <img src="/static/images/like.jpg" alt="Like" /></a></div>
          </div>
          <div class="description" style='width:177px;height:50px;'> <span class="price">{{ deal.price }}</span> {{ deal.description|safe }} <br />
           </div>
           <a href="/usersl/">Add to SL</a>
        </div>
                     </li>
   {% endfor %}
  </ul> 

        </div>

I am developing an e-commerce app based on Django. 我正在开发基于Django的电子商务应用程序。 I want to replace the contents of the DIV with the content got from the server in response from the GET request made to the server. 我想用从服务器获取的内容替换DIV的内容,以响应对服务器的GET请求。 But the problem is whole page is getting loaded into the DIV . 但是问题是整个页面都被加载到DIV中。

Have your view just return HTML suitable for your div, not a complete page. 让您的视图仅返回适合您的div的HTML,而不是完整的页面。

For example, if your http://127.0.0.1:8000/result/ URL is only intended to be used from your AJAX call, then have the view for that URL just render a template without the HEAD, BODY tags etc. 例如,如果仅打算从AJAX调用中使用您的http://127.0.0.1:8000/result/ URL,那么让该URL的视图仅呈现没有HEAD,BODY标签等的模板。

You might have a template something like this: 您可能有一个类似以下的模板:

<ul>
  <li>
    {{ object.value }}
  </li>
<ul>

Note that is doesn't extend from base.html or any other template so when the template is rendered, only the template contents you've specified are rendered. 请注意,它不会从base.html或任何其他模板扩展而来,因此在渲染模板时,仅渲染您指定的模板内容。 There is nothing stopping a user from hitting your URL though, in which case the results returned will not be valid HTML for display in a browser. 但是,没有什么可以阻止用户点击您的URL,在这种情况下,返回的结果将不是在浏览器中显示的有效HTML。

There are alternatives. 还有其他选择。 You could serve out the data from your server using JSON in the AJAX response and build up the DOM from the JSON data. 您可以在AJAX响应中使用JSON从服务器提供数据,并从JSON数据构建DOM。 Or, you could allow the full page to be served as it is now, and use jQuery to replace the contents of your div with only a subsection of the returned page, something like this: 或者,您可以允许按原样提供整个页面,并使用jQuery将div的内容仅替换为返回页面的一部分,如下所示:

$('#result-div').load(URL + ' #container-div');

The jQuery load() function allows you to specify a fragment of the loaded HTML to insert into your div. jQuery load()函数允许您指定要插入到div中的已加载HTML的片段。 In the above example, the contents of the #container-div element in the served HTML would be inserted into the #result-div . 在上面的示例中,投放的HTML中的#container-div元素的内容将插入#result-div The remainder of the served HTML will be discarded. 投放的HTML的其余部分将被丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM