How to get value from form field in django framework?

前端 未结 5 1889
一生所求
一生所求 2020-11-28 05:08

How do I get values from form fields in the django framework? I want to do this in views, not in templates...

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 05:52

    Take your pick:

    def my_view(request):
    
        if request.method == 'POST':
            print request.POST.get('my_field')
    
            form = MyForm(request.POST)
    
            print form['my_field'].value()
            print form.data['my_field']
    
            if form.is_valid():
    
                print form.cleaned_data['my_field']
                print form.instance.my_field
    
                form.save()
                print form.instance.id  # now this one can access id/pk
    

    Note: the field is accessed as soon as it's available.

提交回复
热议问题