tags:

views:

41

answers:

2

I have a very basic contact model. The model has the following fields:

class Entry(models.Model): 
    name = models.CharField(max_length=64, unique=False)
    organization = models.CharField(max_length=100, unique=False, blank=True, null=True)
    team = models.CharField(max_length=64, unique=False, blank=True, null=True)
    position = models.CharField(max_length=64, unique=False, blank=True, null=True)
    address = models.CharField(max_length=130, unique=False, blank=True, null=True)
    ...  

    def __unicode__(self):
     return u'%s' % self.name

I have different templates to display/edit individual entries. I'd like to accomplish the following. When viewing an individual record, I'd like the user to be able to click on "organization" and be redirected to a template that lists all of the existing records in the db from that organization. I've built the template but I'm unsure of the view code.

I feel like it should be something like this, but I don't think it's legal.

def display_organization(request):
    records = Entry.objects.filter(organization__exact=Context)
    t = get_template('org_list.html') 
    html = t.render(Context({'records': records}))
    return HttpResponse(html)

Can anyone assist?

+3  A: 

You probably want the display_organization URL map to include a parameter for the organization:

('^organization/(?P<org_name>.+)$', 'myapp.views.display_organization'),

With that, your display_organization function must accept the org_name parameter too:

def display_organization(request, org_name):
    records = Entry.objects.filter(organization__exact=org_name)
    html = get_template('org_list.html').render({'records': records})
    return HttpResponse(html)
AKX
throws a type error. "display_organization() takes exactly 2 arguments (1 given)"seems not to like the syntax. thanks though.
kjarsenal
You have to make the corresponding change to the URL for this to work. How do you expect your template to know *which* organization to use?
jcdyer
A: 

You're making it quite complex by depending on the URL. It's fine if there's only one or two things that work that way.

I'd say, keep it simple and just use request.GET

def display_organization(request):
    records = Entry.objects.filter(organization__iexact=request.GET['organization'])
    ...
Peter Bengtsson