I think you are on the right track with the jquery setup for handling the 
submission of the form.  What you need to implement here is an AJAX form.  
I would do something like this, although I don't necessarily know what I'm 
doing:

$(document).ready(function () {
var form = $("#gpio_form")
var button = $("#{{pin1.PinID}}")
form.submit(function(e) {
e.preventDefault()
$.get("url that points to your AJAX view/",
        form.serialize(), function(message){
        if (message == 'true'){
        button.attr('value', '1')
} // end if message is true
        if (message == 'false'){
        button.attr('value', '0')
}// end if message is false
});// end get call
});// end form submit
});// end doc ready

So instead of your current view, I would use a dedicated URL and view that 
handles the AJAX GET request.  GET vs. POST request conventions aside, for 
the point of instruction, $.get is easier to use, but is not semantically 
correct.  

The view that handles the button value change would look like something 
like this:
def button_check(request):
    try:
        #something that checks the GET for button status
        if request.GET['button'] == 1:
            result = True
        else:
            result = False
    except:
        result = False
    message = json.dumps({'result':result})
    return HttpResponse(message, mimetype="application/json")

Sorry for the garbage code, but hopefully this will help describe how to 
make an AJAX form and get you most of the way there.  If you want to 
contact me off list, I am willing to help since I'm interested in Django on 
the RaspberryPi

Thanks,

On Monday, February 4, 2013 11:15:19 PM UTC-5, 7equiv...@gmail.com wrote:
>
> I also have a javascript file, but I'm not quite sure what to out in 
> it......
>
>   function gpio_submit(){
>     $("#gpio_results").load("/control_page");
>     return false;
>   }
>   
> $(document).ready(function () {
> $("#gpio_form").submit(gpio_submit);
> });
>
> On Monday, February 4, 2013 11:12:27 PM UTC-5, 7equiv...@gmail.com wrote:
>>
>> I'm controlling the GPIO pins on a raspberryPi that is running a Django 
>> web app. I have a form with only one button that is "On" or "off", it 
>> writes this to a database. The Button's value has to display the current 
>> status "On" or"off".
>> So I need to be able to update the value of the button in the form, 
>> without reloading the entire page.
>>
>> I have jquery, so I'm trying to figure out how to use it in conjunction 
>> with the Django View to get this done. Here is my View.py and 
>> control_page.html
>>
>> I am a novice in Programming so don't be afraid to dumb it down....
>>
>> @login_required
>> def control_page(request):
>>   if request.method == 'POST':   
>>     pin1 = Pin.objects.get(pinID=1)
>>     
>>     if 'pin1Status' in request.POST:
>>       if pin1.pinStatus == 'hi':
>>         pin1.pinStatus = 'low'
>>       else:
>>         pin1.pinStatus = 'hi'
>>       pin1.save()
>>       
>>     variables = RequestContext(request, {'pin1': pin1})      
>>     return render_to_response('control_page.html', variables)
>>   else:
>>     pin1 = Pin.objects.get(pinID=1)
>>     variables = RequestContext(request, {'pin1': pin1})
>>     return render_to_response('control_page.html', variables) 
>>     
>>     
>>     
>>         
>> {% extends "base.html" %}
>> {% block external %}
>>   <script type="text/javascript" src="/media/jquery.js"></script>
>>   <script type="text/javascript" src="/media/jquery_ui.js"></script> 
>>   <script type="text/javascript" src="/media/gpio.js"></script>
>> {% endblock %}
>> {% block title %}Control Panel{% endblock %}
>> {% block head %}
>>
>> {% endblock %}
>> {% block content %}
>>
>>   <div id="gpio_results">
>>   <form id="gpio_form" method="post" action="." >
>>     {% csrf_token %}
>>     <p>{{ pin1.pinDescription}} {{ pin1.pinDirection}}  
>>     <input type="submit" name="pin{{ pin1.pinID }}Status" id="{{ 
>> pin1.pinID }}" value="{{ pin1.pinStatus }}"   />
>>   </form>
>>   </div>
>>   
>>   <iframe src="http://192.168.137.96:8081"; scrolling="no" width="275" 
>> height="220" frameboarder="0">
>>     <p>iframes are not supported by your browser.<p>
>>   </iframe>
>>   
>> {% endblock %}
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to