我有一个带有初始值的表单,但我无法获得要显示的初始值。
def addaward(request):
if request.method == "POST":
form = AwardForm(request.POST)
form.save()
else:
form = AwardForm(initial={'title': 'hi!'})
return render_to_response('award.html',{'form': AwardForm}, context_instance = RequestContext(request))
当我用
删除该行时,真正奇怪的是 form = AwardForm(initial={'title': 'hi!'})
表单仍显示没有错误消息。
Forms.py
from django import forms
from django.forms import ModelForm
from students.models import Student
class AwardForm(ModelForm):
class Meta:
model = Award
Models.py
class Award(models.Model)
date = models.DateTimeField(auto_now_add = True, editable = True)
student = models.ForeignKey('Student')
type = models.CharField('Category', max_length = 30)
title = models.CharField('Title of Award', max_length = 30)
description = models.TextField('Description')
任何想法都会很棒。谢谢!
答案 0 :(得分:0)
这样:
{'form': AwardForm}
应该是:
{'form': form }