2013-08-05 54 views
0

我在寫一個django模板Configuration_Detail.html,它在相關的url上正確呈現。但是,它不會從視圖類中獲取任何變量。我有一個非常相似的模板Configuration_List.html哪些工作正常,但這是一個ListView而不是DetailViewDjango模板沒有收到變量

Configuration_Detail.html:

{% extends "base.html" %} 

{% load i18n %} 

{% block title %}{% trans 'MySite Database' %}{% endblock %} 

{% block branding %} 
<h1 id="site-name">{% trans 'MySite Database: Current Instrumentation Configuration' %}</h1> 
{% endblock %} 

{% block content %} 
Here is some text {{name}} with a variable in the middle. 
{% endblock %} 

頁面呈現的標題欄很好,但內容分塊變爲「這裏是一些文本在中間的變量。」 我相信它應該從這裏採取變量{{ name }}

views.py:

class ConfigurationDetail(DetailView): 
model = Configuration  
def getname(self): 
    name = 'debug' 
    return name 

但它不... 關於如何解決此問題的任何建議,將不勝感激。

編輯補充: Models.py - 配置:

class Configuration(models.Model): 

title = models.CharField(max_length=100,unique=True,blank=False) 
author = models.ForeignKey(User) 
created = models.DateField("date created",auto_now_add=True) 
modified = models.DateField("date modified",auto_now=True) 
description = models.CharField(max_length=512) 
drawing = models.ForeignKey(Drawing,blank=True,null=True) 
instruments = models.ManyToManyField(Instrument) 

def __unicode__(self): 
    return self.title 

get_context_data()方法是使用ctx['author'] = Configuration.author

回答

0

對於DetailView,一個object變量中,它指向數據庫對象的上下文中加入視圖正在渲染。所以在你的模板中你可以這樣做:

{% block content %} 
    Here is some text {{ object.author.get_full_name }} 
    with a variable in the middle. 
{% endblock %} 

get_full_name方法來自User對象。

+0

此外,如果我嘗試使用此方法傳遞特定於某個記錄的項目,它將失敗。我試圖通過'author'的模板,並得到了'' –

+0

發表您的模型。 –

+0

已添加到原始帖子。 –

0

如果我理解正確的,你需要從模板中訪問一個模型屬性,但足以做{{ configuration.author }},而完全不修改上下文數據!

DetailView將所選模型置於上下文中,可用點符號訪問。

+0

這就是我想要做的。不幸的是,它似乎不工作。 –

+0

用你使用'{{configuration.author}}'的非工作代碼編輯問題。順便說一句,在'的getName()'功能是沒有用的;) – caesarsol

+0

'getname'有一個更復雜的工作,但被設置爲通過一個明確的輸出用於測試目的。我原本懷​​疑我的方法可能有問題。當你要求非操作代碼時,你的意思是模板嗎? –