I have a page called view_requests.html
. One one tab on the page, I'd like to have basically a list of objects reddit style that people can upvote. I accomplished this using django-voting
.
However, on a second tab, I would like the ability to add a request. This requires that I have a form that people can submit.
The problem I'm facing is that to implement the Reddit style voting, I had to use the following configuration in my URLs.py (ie. not create my own view for it in views.py). But to create a form, I need to access some code I wrote in views.py. Is it possible to have both on the same page in separate tabs, and how would I go about doing this?
url(r'^view_requests/?$', object_list, dict(queryset=LibraryRequest.objects.all(),
template_object_name='request', template_name='spicemodels/view_requests.html',
paginate_by=15, allow_empty=True), name="view_requests"),
You don't have to use that; object_list
is a built-in Django generic view. Generic being the keyword, because it's merely intended to make it easier for you if your view conforms to a standard. Since, you need more from your view than object_list
can provide, it's time to throw it out and write your own view. You can use object_list
as a guide for creating your own view, but there's a lot of extraneous boilerplate code in it just to make it generic. Simplistically, the following is all you need:
def my_view(request, page=None):
paginate_by = 15
qs = LibraryRequest.objects.all()
paginator = Paginator(qs, paginate_by, allow_empty_first_page=True)
if not page:
page = request.GET.get('page', 1)
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
# Page is not 'last', nor can it be converted to an int.
raise Http404
try:
page_obj = paginator.page(page_number)
except InvalidPage:
raise Http404
render_to_response('spicemodels/view_requests.html', {
'request_list': page_obj.object_list,
'paginator': paginator,
'page_obj': page_obj,
'is_paginated': page_obj.has_other_pages(),
}, context_instance=RequestContext(request))