我在寫一個django模板Configuration_Detail.html
,它在相關的url上正確呈現。但是,它不會從視圖類中獲取任何變量。我有一個非常相似的模板Configuration_List.html
哪些工作正常,但這是一個ListView
而不是DetailView
。Django模板沒有收到變量
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
此外,如果我嘗試使用此方法傳遞特定於某個記錄的項目,它將失敗。我試圖通過'author'的模板,並得到了'' –
發表您的模型。 –
已添加到原始帖子。 –