开发者

Display form in Django not based on Models/Form

My view calls some backend classes which need some user input. When user input is required, i halt processing and store the questions into the session - request.session['questions']. request.session['questions'] is a list of dictionaries. e.g.

request.session['question'] = []
request.session['question'].append({'question' : 'Whats your firstname', 'answer' : ''})
request.session['question'].append({'question' : 'Whats your firstname', 'answer' : ''})

I need to display these questions to the user along with an input box for each question. When the user submits the form, I need to dump the input into the answers part of the session variable.开发者_高级运维 Could someone show me how to do this? I'm a little lost as this isn't really based on Django forms or models as such.

Thanks


You could use forms that aren't associated with models, like this:

class QuestionForm(forms.Form):
    answer = forms.CharField()

def questions(request):
    if request.method == 'POST':
        form = QuestionForm(request.POST) 
        if form.is_valid():
            # Process the data in form.cleaned_data
            return HttpResponseRedirect('/done/')
    else:
        form = QuestionForm() # An unbound form

    return render_to_response('questions.html', {'form': form,})

More documentation here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜