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
(snip)
Pass the template as extra_context from your urls. There are some
examples in the generic views.
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
Works like a charm.
Thanks again.
> Seehttp://docs.djangoproject.com/en/dev/topics/http/urls/#passing-extra-...