Am new to Django but want to learn it and have covered pretty much the basics on the Django website.
Here is my problem:
I have written a python script which presently works in the python shell, but I want to make use of the script on my web. So that when a user goes to my website and provides the neccesary input, clicks submit, the webpage links the input to the python script(which already has input fields like those on the webpage), and the python script runs according to the input given by the user, evaluates it and prints the result of the script on the webpage.
Please help me out guys, counting on you all.
But feel free to suggest other frameworks that could best serve my problem.
It sounds like you would be better off moving the contents of your script into a django view and executing it after your form has been validated.
https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#using-a-form-in-a-view
In your view, you can get the form data using request object. You can pass the form values to your script and send output to your response object.
It seems you didn't completely understand how django works. Please go through the tutorial again.
Related
Goal: I want to send the result of a <script> tag in HTML to a server of any kind.
I am working with the Spotify authorization API for a project and one of the required keys is contained in a query string at the end of my app’s redirect URI. I need to get this key.
My solution is to set the redirect URI to a redirect page. Then, on the page, automatically run a script that gets the current URL and send it to my Python script for use. If there’s another way, please tell me, because I’m pretty stuck.
However, to send variables between HTML and Python, I have found I need to use a simple server. Setting this up is the hard part. I’ve made Java, Node.js, and Python servers, but nothing seems to want to work with the <string> tag, and I’m doubtful something like this would even get the actual output of the script.
Is there a way to do this? This is a pretty long question and I apologize, and it should probably be noted I’m a beginner so an explanation and code examples would be nice. Thank you to anyone who reads this!
I'm in the process of writing a Django app, but I've hit a slight roadblock.
You see, I have the backend return an HTML page, and that works just fine. What I am having trouble with though is going the other way around. I am trying to call a function on the backend once the user clicks a button. Now, I've already searched to see if anyone else has asked this question before, but all the posts that came up had a different scenario linked to them. All of them were for GET requests, but I need to be able to POST some data instead.
Does anyone know how I can do this?
You'll want to get started with Django forms:
https://docs.djangoproject.com/en/3.0/topics/forms/
Forms accept a POST, and there are lots of handy parts for sanitizing the data.
I have a Python script that accepts text from a user, interprets that text and then produces a text response for that user. I want to create a simple web interface to this Python script that is accessible to multiple people at once. By this I mean that person A can go to the website for the script and begin interacting with the script and, at the same time, person B can do the same. This would mean that the script is running in as many processes/sessions as desired.
What would be a good way to approach this?
Maybe you should try a python web framework like django or flask .etc
Make a simple website that offers a webpage that contians a form to input text ,and when people visit the url, put their text in the form and submit, your code can handle it and return a webpage to show the result.
I want to execute a python programme whenever the user clicks on any of the buttons from the django submit_line.html page.
I have tried using variations of the following:
onclick="python script.py"
onclick="/usr/bin/python /home/django/project/script.py"
onclick="python ../../script.py"
but I can't get the script to run.
Does anyone know of any solutions to my problem?
Thanks for your time.
If instantly: use javascript.
If by refreshing the page: just use requests and write your Python code as function in views.py
You don't seem to have understood anything from the Django tutorial or documentation.
In Django, you access the server-side functionality via URLs, which are served by views. You don't reference a Python script directly in your template, you reference a URL which maps to a view, and your Python code goes in that view.
Quite apart from that, onclick is Javascript. You can't just reference a Python script, that makes no sense. You can write a Javascript function to call (via Ajax) a URL which does what you want, or more simply make it a link to that URL.
I would like to know if it is possible to submit a flash form from python and, if it is, how?
I have done form submitting from python before, but the forms were HTML not flash. I really have no idea on how to do this. In my research about this I kept getting 'Ming'. However, Ming is only to create .swf files and that's not what I intend to do.
Any help on this is greatly appreciated.
You can set the url attribute (I think it's url, please correct me if I'm wrong) on a Flash form control to a Python script - then it will pass it through HTTP POST like any normal HTML form.
You've got nothing to be afraid of, it uses the same protocol to communicate, it's just a different submission process.
For your flash app, there's no difference if the backend is python, php or anything, so you can follow a normal "php + flash contact form" guide and then build the backend using django or any other python web framework, receive the information from the http request (GET or POST, probably the last one) and do whatever you wanted to do with them.
Notice the response from python to flash works the same as with php, it's just http content, so you can use XML or even better, JSON.