tags:

views:

22

answers:

2

my form is

class MapForm(forms.ModelForm):
     class Meta:
        model = Map
        fields = ('mapName', 'kmlStr')

and the view is :

map_form = MapForm(request.POST or None)
if map_form.is_valid():
    map = map_form.save(commit=False)
    map.mapName=map_form.mapName#is this code right ?

how to get the mapName 's value , us 'map_form.mapName' ?

thanks

A: 

Since it's modelform, fields mapName and kmlStr are alredy stored in Map object at

map = map_form.save(commit=False)

to get value from form you need to do form.cleaned_data['mapName']

Dmitry Shevchenko
A: 

Not quite, you want to access the form fields through cleaned_data

Processing the data from a form

Once is_valid() returns True, you can process the form submission safe in the knowledge that it conforms to the validation rules defined by your form. While you could access request.POST directly at this point, it is better to access form.cleaned_data. This data has not only been validated but will also be converted in to the relevant Python types for you. In the above example, cc_myself will be a boolean value. Likewise, fields such as IntegerField and FloatField convert values to a Python int and float respectively.

map.mapName = map_form.cleaned_data['mapName']

However, you're already using model forms, so you can save the form to return an object.

map_form = MapForm(request.POST or None)
if map_form.is_valid():
    map = map_form.save()
digitaldreamer