开发者

Display form in Django not based on Models/Form

开发者 https://www.devze.com 2023-01-03 16:50 出处:网络
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

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

精彩评论

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