Reputation: 109
I am working on a django recipe website and one of the pages is currently setup using hidden divs and I believe it is slowing the webpage down quite a bit.
Let me explain further:
I have a page consisting of 6 recipes. each of these recipes is a link and when you click that link it activates a jquery function that turns a hidden div into a div that popups the content over the content container with a opaque black background. The problem with this is that all 6 divs are hidden behind the content container and I believe this is slowing down the sites performance a noticeable amount (especially since I will also be loading comments and perhaps pictures etc).
What i would like to do is use ajax to fetch the information only when necessary instead of having all of the content loaded. (including comments and all information specific to a recipe)
perhaps instead of 6 popup divs i could have just one and load the relevant recipe content into that div.
I am new to ajax and was wondering how exactly this would be implemented with django.
here is the template:
<div id="recipe_cont">
{% for recipe in recipe_list %}
<div id="recipe">
<div id="button{{ forloop.counter }}"> //creates 6 buttons because my page is paginated 6 objects to a page
<h4>{{ recipe.name }}</h4>
<h5>{{ recipe.author }}</h5>
<h5>Prep Time: {{ recipe.prep_time }} minutes</h5>
</div>
<h6><a href="/addrecipe/{{ recipe.id }}">Add Recipe</a>
<a href="/removerecipe/{{ recipe.id }}">Remove Recipe</a></h6>
<div id="popupContact{{ forloop.counter }}" class="popup">// this is the information I want to use ajax to retrieve and place into the popup div
<a id="popupContactClose{{ forloop.counter }}" style="cursor:pointer;float:right;">x</a>
<h1>{{ recipe.name }}
</h1>
<h3>{{ recipe.author }}</h3>
<p id="contactArea">
Ingredients: {{ recipe.ingredients }}
<br/><br/>
Steps: {{ recipe.steps }}
</p>
<br/><br/><br/><br/>
<br/>
{% get_comment_form for recipe as form %}
<table>
<form action="{% comment_form_target %}" method="post">
{% csrf_token %}
{{ form }}
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Post" />
<input type="submit" name="preview" value="Preview" />
</td>
</tr>
</form>
</table>
{% for comment in comment_list %}
<a name="c{{ comment.id }}">comment</a>
{% endfor %}
</div>
<div id="backgroundPopup">
</div>
</div>
{% endfor %}
</div>
here is the relevant jquery code: this code is just to demonstrate how the popup div works - don't think you really need to see this
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: [email protected]
//@license: Feel free to use it, but keep this credits please!
/***************************/
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup($contact_selector){
//loads popup only if it is disabled
if(popupStatus==0){
$("#backgroundPopup").css({
"opacity": "0.7"
}).fadeIn("slow");
$contact_selector.fadeIn("fast");
popupStatus = 1;
}
}
//disabling popup with jQuery magic!
function disablePopup($contact_selector){
//disables popup only if it is enabled
if(popupStatus==1){
$("#backgroundPopup").fadeOut("slow");
$contact_selector.fadeOut("slow");
popupStatus = 0;
}
}
//centering popup
function centerPopup($contact_selector){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("body").height();
var popupWidth = $("body").width();
//centering
$contact_selector.css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6
$("#backgroundPopup").css({
"height": windowHeight
});
}
$(document).ready(function(){
//LOADING POPUP
//Click the button event!
$("#button1").click(function(){
//centering with css
centerPopup($('#popupContact1'));
//load popup
loadPopup($('#popupContact1'));
});
$("#button2").click(function(){
//centering with css
centerPopup($('#popupContact2'));
//load popup
loadPopup($('#popupContact2'));
});
$("#button3").click(function(){
//centering with css
centerPopup($('#popupContact3'));
//load popup
loadPopup($('#popupContact3'));
});
$("#button4").click(function(){
//centering with css
centerPopup($('#popupContact4'));
//load popup
loadPopup($('#popupContact4'));
});
$("#button5").click(function(){
//centering with css
centerPopup($('#popupContact5'));
//load popup
loadPopup($('#popupContact5'));
});
$("#button6").click(function(){
//centering with css
centerPopup($('#popupContact6'));
//load popup
loadPopup($('#popupContact6'));
});
//CLOSING POPUP
//Click the x event!
$("#popupContactClose1").click(function(){
disablePopup($('#popupContact1'));
});
$("#popupContactClose2").click(function(){
disablePopup($('#popupContact2'));
});
$("#popupContactClose3").click(function(){
disablePopup($('#popupContact3'));
});
$("#popupContactClose4").click(function(){
disablePopup($('#popupContact4'));
});
$("#popupContactClose5").click(function(){
disablePopup($('#popupContact5'));
});
$("#popupContactClose6").click(function(){
disablePopup($('#popupContact6'));
});
$("#backgroundPopup").click(function(){
disablePopup($('.popup'));
});
//Press Escape event!
$(document).keyup(function(e) {
if( e.which == 27 ){
disablePopup($('.popup'));
}
});
});
and finally the css: this is also a bit irrelevant but helps to show how the jquery works
#backgroundPopup{
display:none;
position:fixed;
_position:absolute; /* hack for internet explorer 6*/
height:100%;
width:100%;
top:0;
left:0;
background:#000000;
border:1px solid #cecece;
z-index:1;
}
#popupContact1, #popupContact2, #popupContact3, #popupContact4, #popupContact5, #popupContact6{
display:none;
position:fixed;
_position:absolute; /* hack for internet explorer 6*/
height:400px;
width:420px;
background:#eee76f;
border:2px solid black;
z-index:2;
padding:12px;
font-size:13px;
border-radius: 15px;
}
.popup{
overflow:auto;
}
.show_hide{
float:left;
}
#popupContact1 h1, #popupContact2 h1, #popupContact3 h1, #popupContact4 h1, #popupContact5 h1, #popupContact6 h1{
text-align:left;
color:black;
font-size:22px;
font-weight:700;
border-bottom:1px dotted black;
padding-bottom:2px;
margin-bottom:20px;
}
#popupContactClose1, #popupContactClose2, #popupContactClose3,#popupContactClose4,#popupContactClose5,#popupContactClose6{
font-size:14px;
line-height:14px;
right:6px;
top:4px;
position:absolute;
color:black;
font-weight:700;
display:block;
cursor:pointer;
}
#button6,#button5,#button4,#button3,#button2,#button1 {
text-align:left;
cursor:pointer;
width:150px;
}
please let me know if there is anything else I should have included
thank you
snackerfish
Upvotes: 0
Views: 591
Reputation: 5841
There is perfect ajax functions in jQuery. For your case you can use something like
$('#recipe_cont').load('/recipe/123/ajax/')
Actually you should use one URL for both ajax and non-ajax access but it's easier to separate them for learning. /recipe/123/ajax/
will return just part of the page, <div id="recipe">...</div>
with all recipe content.
That's it. You can also add client side caching and other stuff, but basically this would work fine enough.
Upvotes: 2