submit parameters to py script

53 views
Skip to first unread message

Smaran Harihar

unread,
Jul 6, 2012, 6:20:59 PM7/6/12
to django...@googlegroups.com
Hey Djangoers,

I am trying to execute a python script on submit button on a html file. 

So i have created a view which renders a html template file and on 'submit', I wish to send few parameters to the py script.

I wanted to know,

1. where should I keep my py script?
2. I have not modified my html into Django template, but what modifications will it need if I want to send parameters to py script on submit?

--
Thanks & Regards
Smaran Harihar

Rohan

unread,
Jul 6, 2012, 9:14:04 PM7/6/12
to django...@googlegroups.com
For a cgi script or a wsgi script, all you need to do is add a script alias in your apache conf (if you're using apache). The parameters should be available as a a dictionary in both the cases (cgi.FieldStorage() and envrion).

Although it would help to know why you want to do this. If you already have django up and running, you could just implement the functionality of your script as a view function
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Smaran Harihar

unread,
Jul 6, 2012, 9:19:01 PM7/6/12
to django...@googlegroups.com
Thanks for the reply Rohan. I am trying to create a Django App in which, user will be adding some info in the text box and on clicking submit, I wish to send the contents of the textbox to the py script. The output of the py script, I wish to display it in a table on my HTML template.

I hope I was able to explain myself. Also where should I place my py sript, which will be triggered on submitting the data.

Thanks,
Smaran 

Babatunde Akinyanmi

unread,
Jul 7, 2012, 3:15:56 AM7/7/12
to django...@googlegroups.com
Hi,
Just like Rohan said, why not do that in your view when processing your form?

Here's from the documentation:
https://docs.djangoproject.com/en/1.4/topics/forms/?from=olddocs
Sent from my mobile device

Melvyn Sopacua

unread,
Jul 7, 2012, 4:42:33 AM7/7/12
to django...@googlegroups.com
On 7-7-2012 0:20, Smaran Harihar wrote:

> I am trying to execute a python script on submit button on a html file.
>
> So i have created a view which renders a html template file and on
> 'submit', I wish to send few parameters to the py script.
>
> I wanted to know,
>
> 1. where should I keep my py script?

Anywhere you want. It's probably best you describe what your script
does. We don't really understand why you want to execute a script and
not just put that code in a view function.
Other than that, a python script you want to execute is no different
than a sh script or binary command, so you'll need to use python's
subprocess or os module functions, pass arguments in proper shell syntax
and read output.
--
Melvyn Sopacua


Smaran Harihar

unread,
Jul 7, 2012, 5:44:21 AM7/7/12
to django...@googlegroups.com

Thanks for the reply Melvyn and Babatunde. So my script is, using the parameters, to search in a database and then spitting out the output back as CSV.
I wish to return the CSV and also display the output in the table on my html template.
Ok, so adding it to view makes more sense, but how do i connect or execute that view, on the click of the submit button??

Babatunde Akinyanmi

unread,
Jul 7, 2012, 6:28:52 AM7/7/12
to django...@googlegroups.com
Errr, have you read this:
https://docs.djangoproject.com/en/1.4/howto/outputting-csv/

Your view can know if a form was submitted, submitted but with invalid
data and submitted with valid data. A view is just a python function
so if you have determined that the user submitted valid data, just go
ahead and extract the CSV to be output.
Perhaps you should go through the examples in the django forms
documentation again:
https://docs.djangoproject.com/en/1.4/topics/forms/?from=olddocs

However, I don't think its possible to send both an html page and a
CSV so I guess you'll have to choose one or send them one at a time
(html page then csv). I'm sure Mevlyn will know if its possible
though.

On 7/7/12, Smaran Harihar <smaran....@gmail.com> wrote:

Melvyn Sopacua

unread,
Jul 7, 2012, 7:01:05 AM7/7/12
to django...@googlegroups.com
On 7-7-2012 11:44, Smaran Harihar wrote:
> Thanks for the reply Melvyn and Babatunde. So my script is, using the
> parameters, to search in a database and then spitting out the output back
> as CSV.

This only makes sense in the django world, if your script is accessing a
database that is not part of the application itself. For example an
accounting program in django that replaces a firms older software.
Access to previous years is then provided via a script that simply
provides CSV format so data can be entered into a spreadsheet program
and reports can be created. One would use this approach if converting
the old data to the new application consumes too many resources for the
few times the data will be used.

I can't think of many other cases where I would use this approach, so ...

> Ok, so adding it to view makes more sense, but how do i connect or execute
> that view, on the click of the submit button??

The key question is if you have models for the tables in this database?
If so your best option is to translate the parameters to django's
queryset syntax. If you don't understand what that means, you're
probably new to django and we'll try to be more verbose.

As a side note, especially when new to django but not unfamiliar with
webdevelopment/databases, you need to learn to let go of your SQL
knowledge and start thinking in models and querysets. Once you fully
understand how this works and what you can do with it, you can apply
your SQL knowledge to optimize what django's syncdb has created for you
or even start working the other way around using inspectdb.
--
Melvyn Sopacua


Smaran Harihar

unread,
Jul 7, 2012, 8:09:54 AM7/7/12
to django...@googlegroups.com
Once again thanks for the helpful guidance Melvyn and Babatunde.

Babatunde: I will go through the links you provided and come back with new queries.

Melvyn : Ya, I am a newbie in Django but have Web Development exp. There is one problem when I wish to create models and queryset for connecting to the database and that is like this,

1. My settings.py points to Postgres database, which is my main database.
2. The data I wish to extract and display in CSV, is in another database (mysql). What I have come to know after completing the basic tutorials of Django is that we define the database in the settings.py and since I already have the postgres defined, how can I also connect to this MySql Database, using views/models? This is why I wanted to trigger an external py script which will deal with this MySql database separately.

Is there a way to connect to multiple databases simultaneously? If so then please let me know how?

Thanks,
Smaran

--
Melvyn Sopacua


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Daniel Walz

unread,
Jul 7, 2012, 10:37:24 AM7/7/12
to django...@googlegroups.com
Hi Samir and Babatunde,


2012/7/7 Babatunde Akinyanmi <tunde...@gmail.com>:
> However, I don't think its possible to send both an html page and a
> CSV so I guess you'll have to choose one or send them one at a time
> (html page then csv). I'm sure Mevlyn will know if its possible
> though.
>

I think so too, one server request delivers one response.
But you can achieve this, if you place a link on top of the html view
of your page.
And if you are sure, *everybody* who accesses the page wants to
download the csv as well, you can put a redirect in the http-meta tags
to the csv view. You have probably seen this practice on pages like
"sourceforge" etc. : "If your download doesn't start automatically,
click this link..."

Hope that helps, best regards

Daniel
Reply all
Reply to author
Forward
Message has been deleted
0 new messages