一尘不染

如何从Django框架的表单字段中获取价值?

django

如何从django框架的表单字段中获取值?我想在视图中执行此操作,而不是在模板中执行此操作…


阅读 444

收藏
2020-03-30

共1个答案

一尘不染

在视图中处理表单的标准模式如下所示:

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...

            print form.cleaned_data['my_form_field_name']

            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render_to_response('contact.html', {
        'form': form,
    })
2020-03-30