Django从模型中检索价值

时间:2012-06-30 23:36:59

标签: django-forms djangoappengine

我有这个model.py:

from google.appengine.ext import db

class Poem(db.Model):
    title = db.StringProperty()
    words = db.StringListProperty()
    created_on = db.DateTimeProperty(auto_now_add = 1)
    created_by = db.UserProperty()

    def __str__(self):
        return str(self.words)

    def get_absolute_url(self):
        return '/poem/%s/' % self.key()

以下观点:

def index(request):
    poems = models.Poem.all().order('-created_on').fetch(20)
    payload = dict(poems = poems)
    return render('index.html', payload)

def create(request):
    if request.method == 'GET': 
        poemform = bforms.PoemForm()

    if request.method == 'POST':
        poemform = bforms.PoemForm(request.POST)
        if poemform.is_valid():
            poem = poemform.save()
            return HttpResponseRedirect(poem.get_absolute_url())

def poem(request, poem_key):
    poem = models.Poem.get(poem_key)
    payload = dict(title = poem.title, words = poem.words)
    return render('poem.html', payload)

当我通过创作形式保存一首新诗时,这首诗会在侧栏,最近的诗歌以及索引页面上的所有诗歌中显示出来。但是,当我单击其中一个链接时,标题和单词似乎都没有填充到模板中。

1 个答案:

答案 0 :(得分:0)

我找到了答案......我正在将单独的标题和单词传递给模板,在那里它只期待一首诗。