I'm almost done with my site except for the last part where I need to make the gallery page support ajax to change the page number using Ajax.
Gallery pages view:
def gallerypages(request, page):
items = Example.objects.all().order_by('-pk')
categories = Categorie.objects.all()
paginator = Paginator(items, 12)
try:
itemsList = paginator.page(page)
except PageNotAnInteger:
itemsList = paginator.page(1)
except EmptyPage:
itemsList = paginator.page(paginator.num_pages)
if items.count()>1:
return render_to_response('gallery.html', {'items': itemsList,'categories': categories,}, context_instance = RequestContext(request))
Dajax/Dajaxice are not very well documented... I only need to show some images.
Here's how to do it with Dajax/Dajaxice, which are meant to make AJAX easy in Django:
Install Dajaxice and Dajax per the documentation. The docs don't seem to mention it, but you can also use
pip
, i.e.to the get the libraries. In any case, make sure to follow the doc instructions to install the Django apps and get the necessary Javascript libraries loaded into
gallery.html
. (Note you need to have jQuery or a similar JS framework installed for Dajax to work.)In
gallery.html
, isolate the section where theitems
andcategories
are rendered into HTML. Copy this section into a separate Django template called, say,gallery_content.html
and then replace the section ingallery.html
with a blank<div>
with a specific id, e.g.What you are doing is creating
#gallery-content
as a placeholder for the HTML that will be generated later for each page via a Dajaxice call.Now, elsewhere in
gallery.html
, create a way for the user to tell you what page to go to, e.g.The Javascript
onclick
code -- which is called whenever the user clicks on the button element -- does two things: (1) grabs the value of the#page-number
input element, and (2) sends it to the Djangogallerypages_content
view asynchronously, i.e. without a normal web browser page load, via theDajaxice.myapp.gallerypages_content
Javascript call. Note thatmyapp
should be replaced with the name of your Django app.Finally, you need to create the
gallerypages_content
view -- which is a variant of your existinggallerypages
view modified to work with Dajaxice/Dajax. Dajaxice is hard-coded to look for such views inajax.py
, so createajax.py
in yourmyapp
folder as follows:This is what the code above does: (1) converts the
page
parameter, which is now a string (i.e the raw string value of the#page-number
input element), into a Python integer; (2) does the same calculation as before to getitemsList
andcategories
; (3) usesrender_to_string
to rendergallery_content.html
to an HTML string instead of to the normal Django HTTP response; (4) uses the Dajax API to create an instruction to inject the HTML into the#gallery-content
div; (5) and, as the view's response, returns these instructions in JSON format. The Dajaxice call in theonclick
handler will in effect receive these instructions and act on them (strictly speaking, it's theDajax.process
callback that does this), causing the HTML to show up. Note that you need to decorategallerypages_content
with@dajaxice_register
-- that helps Dajaxice hook everything together.I haven't tested any of this specifically, but it's based on how I've gotten Dajaxice/Dajax to work for me and I hope it works for you -- or at least gets you started!
https://github.com/jorgebastida/django-dajax/