我认为这应该很简单,但我似乎无法找到我正在寻找的答案。我正在基于模板中的外键/下拉设置会话变量。会话变量保存为主键而不是字段名称。我该如何改变?
模型:
class Verbs(models.Model):
verb = models.CharField(max_length=20)
def __unicode__(self):
return self.verb
class MyVerbs(models.Model):
verb = models.ForeignKey(Verbs)
形式:
class MyVerbForm(forms.ModelForm):
class Meta:
model = MyVerbs
的观点:
def TestSession(request):
request.session['myverb'] = ''
if request.method == 'POST':
form = MyVerbForm(request.POST)
if form.is_valid():
myvariable = form.cleaned_data['verb']
#myvariable = request.POST.get('verb', '')
request.session['myverb'] = myvariable
return HttpResponseRedirect('/')
else:
form = MyVerbForm(request.POST)
context = {'form': form}
return render_to_response('test.html', context, context_instance=RequestContext(request))
def会话(请求): myvariable = request.session ['myverb'] context = {'myvariable':myvariable} return render_to_response('base.html',context,context_instance = RequestContext(request))
答案 0 :(得分:0)
您应该致电form.is_valid()
,然后从form.cleaned_data
而不是request.POST
获取价值。