Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

python - Write value on Input type from Django Form

I read a lot of questions in this page but I don't find information about this problem. I have a form created automatically for django. It's working well but I need to write values on input when I find values, is it possible??

I am trying this, if I print misHoras.horas_trabajo I can read the good value, but I don't know how can I write this value on input html type.

Thanks.

def horas(request):

#horas_todas = Horas.published.all()
horas = get_object_or_404(Horas,
                        autor= '2',
                        dia='2021-01-26',
)
 
if request.method=='POST':
    misHoras=FormularioHoras(request.POST)
        
    if misHoras.is_valid():
        infForm=misHoras.cleaned_data
        misHoras.horas_trabajo = horas.horas_trabajo
        return render(request, "BieleGastosApp/formulario_horas.html", {"form": misHoras})
else:
    misHoras = FormularioHoras()
            
#return render(request, "BieleGastosApp/horas.html", {"horas": horas})
return render(request, "BieleGastosApp/horas.html", {"form": misHoras})
question from:https://stackoverflow.com/questions/66060593/write-value-on-input-type-from-django-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the django documentation: https://docs.djangoproject.com/en/3.1/ref/forms/api/#accessing-clean-data

If you have a bound Form instance and want to change the data somehow, or if you want to bind an unbound Form instance to some data, create another Form instance. There is no way to change data in a Form instance. Once a Form instance has been created, you should consider its data immutable, whether it has data or not.

so what you would do is to define a new dictionary, update it with misHoras.cleaned_data, and initialize a new form with that data:

if misHoras.is_valid():
    new_form_data = {}
    new_form_data.update(misHoras.cleaned_data)
    new_form_data['horas_trabajo'] = horas.horas_trabajo
    new_form = FormularioHoras(new_form_data)
    if new_form.is_valid():
        return render(..., {"form": new_form})

You could theoretically also copy POST first, and update it with fixed data, or add horas.horas_trabajo as an extra variable to the template, since it is obviously not supposed to be changed by your logic, in the form, anyway (as you overwrite any input on that field); however if you wanted to add it as a changeable field, you would provide it with the initial form instead.

to make it more beautiful, you might want to work directly in the Form class, creating your own validations instead, that retrieve the data or introduce and use Serializers, but maybe this solution is enough for your particular problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...