How to execute python code by django html button? -
I want to execute my Pyro code by onclick. I'm getting the results after running the server. My button is not working here. My code is
url -
url (R 'index / $', index ), index.html -
& lt; Html & gt; & Lt; Body & gt; & Lt; Form action = "/ index /" method = "GET" & gt; & Lt; Input type = "submit" value = "click" & gt; & Lt; / Form {{}} production & lt; / Body & gt; & Lt; / Html & gt; Importing from django.shortcuts import, pythoncode import mycode def index render_to_response: if request.method == "import" from views.py -
GET ": py_obj = mycode.test_code (10) py_obj.code () submission of return (request, 'index.html', {" output ": py_obj.a}) I Another application to separate the created Python code - My name of the application name Python code and file name
class test_code: def __init __ (self, a): self.a = a Self.b = 4def code (self): back to self A, self.b Please help me I'm new in Django Thanks in advance
If you want to help other people to help you, then you have to help them first. First things first: Qualifications COUNTS Please think about all of us, we are just human, I'm getting a headache while reading your HDL code.
Brendon suggested to you and correct the errors in your code and the HTML code is indented. Please.
Now let's see what we can do for you.
If you just want to click and display something on the fly on your page, you will need JavaScript and AJAX. There is no need to create the entire form for a single button.
You can use this snippet in your index:
& lt; Button ID = "MyClickButton" type = "button" & gt; Click & lt; / Button & gt; & Lt; Div id = "myOutput" & gt; & Lt; / Div & gt; Now trigger something while clicking on the button:
$ ("# myClickButton"). Click (function () {$ .get ("/ output /", function (data) {$ ("# myOutput") .html (data);}, "html");}); The above code is jQuery. Please read the official documentation of jQuery.
DIF index (request): submission of returns (request, 'yourapp / index.html') A folder inside your app Remember to remember your template in the template . It should look like this: - yourproject | | --Urpp | ---- templates | ------ Your App | -------- index.html Create another view in your thoughts: Def output (request): if request.is_ajax (): py_obj = mycode.test_code (10 ) Submit the Return (Request, 'yourapp / output.html', {'Output': py_obj .a})
Your output can be HTML this way:
& lt; P & gt; {{Output}} & lt; / P & gt; All this is not a header, no body, nothing will be put per AJAX on flying in this code index.html.
Now your method code : analyze the deck code (self)): back to self. A, self.b Do you know what happens here? You can only return one value to the function. You think that you are returning to A and B as an integer. wrong! This method gives a tube with two elements. This method will return (10, 4) . When you call this method in your index, it just gives this to Tuple, but you are not specifying it in a variable, so it will run from the wind. This is worthless calls. I hope this will give you an idea how you can do it. If you do not want to use Javascript (and AJAX) you can send your form per post and make a difference in your view:
DIF index (request): If request.method == 'GET': Submit Return (Request, 'yourapp / index.html', '' Output ':' '}) Elife Request. Method == 'post': py_obj = mycode.test_code (10) Submit the return (request, your app / output. Html ', {' output ': py_obj.a}) In this case you will not need view output and output. You can use your index.html inside the form.
Comments
Post a Comment