0
votes

I think this should be simple, but I can't seem to find the answer I'm looking for. I'm setting a session variable based on a foreign key/ drop down in the template. The session variable saves as the primary key instead of the field name. How do I change this?

models:

class Verbs(models.Model):
    verb = models.CharField(max_length=20)

    def __unicode__(self):
        return self.verb

class MyVerbs(models.Model):
    verb = models.ForeignKey(Verbs)

forms:

class MyVerbForm(forms.ModelForm):

class Meta: 
    model = MyVerbs

views:

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 Session(request): myvariable = request.session['myverb'] context = {'myvariable': myvariable} return render_to_response('base.html', context, context_instance=RequestContext(request))

1

1 Answers

0
votes

You should call form.is_valid() then get the value from form.cleaned_data rather than request.POST.