Calling the same views, displaying in different templates

5 views
Skip to first unread message

rvidal

unread,
Apr 12, 2010, 11:59:54 PM4/12/10
to Django users
Hi,

I have a database of related data connected via foreign keys. One to
many relationships between one table and various other tables.

Random example:
Client
- id
- name
- age

Receipts
- id
- client (fk)
- timestamp

Contacts
- id
- client (fk)
- timestamp

I have my urls.py looking something like this:
urlpatterns = patterns('mysites.shop.views',
(r'^$', 'index'),
(r'^client/(?P<client_id>\d+)/$', 'details'),
(r'^client/(?P<client_id>\d+)/receipts/$', 'receipts'),
(r'^client/(?P<client_id>\d+)/contacts/$', 'contacts'),
)

I have similar data displayed on my details, receipts and contacts
page because they are related. So I show in the main content div the
client details and receipts and contacts in side bar on the details
view, then I swap the relevant section to the main div depending on
the view I want.

The thing here is that I basically just use the same view function,
but move things around in the template. Should I be repeating the
views and creating a template for each situation?

Here's what my views.py looks like (continuing with random example):
from django.shortcuts import render_to_response, get_object_or_404
from lymphoma.ldata.models import Patient, Pathology

def index(request):
client_list = Clients.objects.all().order_by('-timestamp')
return render_to_response('shop/index.html', {'client_list':
client_list})

def details(request, client_id):
c = Clients.objects.get(pk=client_id)
receipt_list = c.receipts_set.all()
contacts_list = c.contacts_set.all()
return render_to_response('shop/details.html', {'client': c,
'receipt_list': receipt_list, 'ccontact_list': contact_list})

def receipts(request, client_id):
c = Clients.objects.get(pk=client_id)
receipt_list = c.receipts_set.all()
contacts_list = c.contacts_set.all()
return render_to_response('shop/receipts.html', {'client': c,
'receipt_list': receipt_list, 'ccontact_list': contact_list})

def receipts(request, client_id):
c = Clients.objects.get(pk=client_id)
receipt_list = c.receipts_set.all()
contacts_list = c.contacts_set.all()
return render_to_response('shop/contacts.html', {'client': c,
'receipt_list': receipt_list, 'ccontact_list': contact_list})


Note that all I change in these views is the template.

So, what am I doing wrong here? What I currently have works, but I'm
sure I'm not proceeding properly.

Any help would be much appreciated. Thank you in advance.

Cheers,
Ricardo

bruno desthuilliers

unread,
Apr 13, 2010, 5:51:54 AM4/13/10
to Django users
On 13 avr, 05:59, rvidal <rvi...@gmail.com> wrote:
> I have my urls.py looking something like this:
> urlpatterns = patterns('mysites.shop.views',
>         (r'^$', 'index'),
>         (r'^client/(?P<client_id>\d+)/$', 'details'),
>         (r'^client/(?P<client_id>\d+)/receipts/$', 'receipts'),
>         (r'^client/(?P<client_id>\d+)/contacts/$', 'contacts'),
> )

(snip)

Pass the template as extra_context from your urls. There are some
examples in the generic views.

Danny Adair

unread,
Apr 13, 2010, 12:30:10 AM4/13/10
to django...@googlegroups.com
On Tue, Apr 13, 2010 at 15:59, rvidal <rvi...@gmail.com> wrote:
>[...]

> I have my urls.py looking something like this:
> urlpatterns = patterns('mysites.shop.views',
>        (r'^$', 'index'),
>        (r'^client/(?P<client_id>\d+)/$', 'details'),
>        (r'^client/(?P<client_id>\d+)/receipts/$', 'receipts'),
>        (r'^client/(?P<client_id>\d+)/contacts/$', 'contacts'),
> )

You could just pass an additional parameter:
===========================================
       (r'^client/(?P<client_id>\d+)/$', 'myview', {'template_name':
'details'}),
       (r'^client/(?P<client_id>\d+)/receipts/$', 'myview',
{'template_name': 'receipts'}),
       (r'^client/(?P<client_id>\d+)/contacts/$', 'myview',
{'template_name': 'contacts'}),
===========================================
def myview(request, client_id, template_name):
===========================================
See
http://docs.djangoproject.com/en/dev/topics/http/urls/#passing-extra-options-to-view-functions

Or let myview figure out the template from the request path.

Cheers,
Danny

rvidal

unread,
Apr 13, 2010, 10:19:09 AM4/13/10
to Django users
Thanks! I knew there was a way around this. Repeating code is very not
like django :)

Works like a charm.

Thanks again.

> Seehttp://docs.djangoproject.com/en/dev/topics/http/urls/#passing-extra-...

Reply all
Reply to author
Forward
0 new messages