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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to