1

I'm making a web interface for my Raspberry Pi using Django.

I'm trying to execute python code which lights up a display by simply clicking a HTML button inside a Django Template.

I currently light up the LED display by running this on command line:

cd rpi/
sudo python bubbles.py

Which executes that ^ python code However I might want like to change the .py file depending on the HTML ID:

sudo python file_name.py

Anyways, this script needs to be executed via web browser so I wrote a view ...but I have no idea how to link a script to a view. My template:

graphics_list.html

{% extends "base.html" %}
{% block content %}
<h1>{{ title }}</h1>
<h1> Scrolling Raspberry Pi LED Selection </h1>
{% for obj in object_list %}

<h3><a href='{{ obj.get_absolute_url }}'>{{ obj.name }}</a></h3>

  <img src="{{ obj.image.url }}" alt="..." class = "img-responsive"><br>
  <p><a href="{{ need_to_execute_script_somehow }}" class="btn btn-primary" id = "{{ obj.name }}" role= "button">Use</a></p>

{% endfor %}
{% endblock content %}

I really don't know where to begin, should I use AJAX for this, PHP?

user1920076
  • 103
  • 1
  • 1
  • 14
  • You want each link to be prefixed with something. For example, `/script/`. Then, using Django, route requests to `/script/` to execute the file `name`. – Rushy Panchal May 24 '16 at 03:13

1 Answers1

3

Instead of answering your code question, I'll answer your approach question.

To run this script from the internet, you need a web server. This will be the agent that handles your web (HTTP) requests. These come on all sorts of flavours. Django is a web framework, designed for creating complex web sites easily. Django also comes with a web server which it uses to serve its content.

The most relevant part to you will be this: Django is overkill for your goal. It may cause confusion, but if you've already got it up and running, it may also be best to stick with it. If you're not comfortable with running a webserver, using Django's may save you time.

I'd start by breaking down the problem into smaller goals, like this:

  • Get the Django webserver started. Just enough to see some kind of Django sample page when you access the server from your web browser.
  • Learn how to edit the models.py so that you can "render" (show in the browser) whatever view you want.
  • Make the VIEW mentioned above have a link to another url (still on your web server). This will trigger a request to your server for a different url. This url will be handled by a different method in your models.py, which in turn will render some other view. For now, imagine this other view displaying "Yes, youve lit the LED" or "LED lighting completed". The new method needed to render this view will be where you hook your code into for running your script.
  • Instead of running the script, just print something to the console to ensure that your "hook" is working. print "***** SOON ILL REPLACE THIS WITH WHATEVER IS NEEDED TO RUN MY SCRIPT ****" and look for that in the console
  • Then learn how to execute python from Django, and drop it in there.

The solution is here: Executing Python script from Django shell

What about AJAX? Ajax for you will result in a slight user-interface improvement. I'd forget it entirely until you've got the core solution working. It is by no means required to run a script from the web.

Community
  • 1
  • 1
mcsilvio
  • 1,098
  • 1
  • 11
  • 20
  • I'm currently just running my django app on the django development server. Later on I'll switch to another type of webserver. – user1920076 May 24 '16 at 03:26
  • Thank you for the reply, I'll look into trying to get it to run from the Django Shell and then implement it into my view somehow. And add the additional logic for LED light views – user1920076 May 24 '16 at 03:27