I’m pretty new with Django and the whole web-developing concept. I’ve only taken Java and C++ , but I got a job working as a web-developer at my university. I’m currently trying to implement a form – (http://albedo.csrcdev.com/pages/submit). In my models, I have one more field that doesn’t show up on my form, which is called Albedo. Albedo is supposed to be calculated by sum( outgoing1, outgoing2, outgoing3 ) / sum( incoming1, incoming2, incoming3 ). So my question is, how and where do I take those variables from the database, and assign the new calculated value to Albedo.
My co-worker told me to use ModelForm for my form, and try doing it in views.py but now I’m sitting here stuck and clueless and he just left for vacation! 🙁
Thanks in advance,
David
views.py
@login_requried
def submit( request ):
if request.method =='POST':
form = DataEntryForm( request.POST )
model = DataEntry( )
if form.is_valid():
form.save()
return HttpResponseRedirect('/map/rebuild/')
else:
form = DataEntryForm( )
return render_to_response(
'pages/submit.html', { 'form': form },
context_instance=RequestContext(request) )
Note that you don’t need to instantiate
model = DataEntry()manually – ifDataEntryFormis aModelFormsubclass, it’ll create the model when you call.save().It would probably be a good idea to encapsulate the calculation in a
DataEntry.update_albedo()method or something. You would call that beforeinstance.save()instead doing the calculation in the view itself.