2009-12-14 111 views
0

我有一個非常基本的聯繫模式。該模型有以下字段:查詢過濾器中的上下文?

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 

我有不同的模板來顯示/編輯單個條目。我想完成以下內容。 查看單個記錄時,我希望用戶能夠單擊「組織」並將其重定向到一個模板,該模板列出該組織中數據庫中的所有現有記錄。我已經構建了模板,但我不確定視圖代碼。

我覺得它應該是這樣的東西,但我不認爲這是合法的。

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) 

任何人都可以協助嗎?

回答

3

你可能想在display_organization URL映射到包括爲組織一個參數:

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

就這樣,你display_organization函數必須接受ORG_NAME參數太多:

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) 
+0

拋出類型錯誤。 「 」display_organization()只需要2個參數(給出1)「 似乎不喜歡的語法。謝謝。 – kjarsenal 2009-12-14 19:23:16

+0

您必須對URL進行相應的更改才能生效。您如何期望您的模板知道*要使用哪個組織? – jcdyer 2009-12-14 21:14:08

0

你讓它取決於網址很複雜。如果只有一兩件事情可以這樣工作,那沒問題。

我會說,保持簡單,只是使用request.GET中

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