1

I'm making a Django web app where I want to take user input from an HTML textbox and run it through a python script. The script works when I input text from the command line, but I want to be able to make the web app print what happens to the text when it goes through the script.

I have already made a Django web app, and when the server is run a basic HTML page with a simple text box and a submit button appears (it's a form).

I have written a separate python script which will take user input (from the command line) and run the text entered through the script.

How would I be able to replace the input() from the function with form data from the HTML page?

An example of what I want to do:

<form action="">
text: <input type="text" name="handle" value=""><br>
<input type="submit" value="Submit">
</form>

The above is a simple form with the submit button. When the 'submit' button is pressed, I want that data to go to a script:

import statements

def blah():
screenname = input('Enter your name here: ') 
output = screenname.functionA
print(output)

Except the input() is replaced with the form data from the HTML file.

Where would I store the python script in the Django app?

How would I pass the text from the form to the python script?

Any help is appreciated, I'm trying to develop my web design abilities one step at a time :)

2 Answers 2

3

Lets start with form. You will need to post that form to a django view:

<form method="POST" action="">
  <inputs here ...>
</form>

Inside the view where you render that page you will need to check for method of the request and if it is a POST request, then pass it to the function.

def view_name(request):
  if request.method == "POST":
    screenname = request.POST.get("handle", None)  # handle is the name of the input in the question.
    # Here you can do anything with your screenname, like passing it to the function.
  return render(request, 'path/to/form.html', {})

Now about putting your custom script inside django. One way will be adding utils.py script inside you app and calling utils.blah(sreenname) in previous code. Also instead of print you can return the result and show it:

from app_name.utils import blah
def view_name(request):
  if request.method == "POST":
    screenname = request.POST.get("handle", None)
    result = blah(screenname)
    return render(request, 'path/to/result.html', {'result': result}
  return render(request, 'path/to/form.html', {})
Sign up to request clarification or add additional context in comments.

10 Comments

In regards to the last bit of code: is that all in the Django view?
Yes. Think as Model View Controller logic, django is Model Template View type framework and this is work for View
What would the urls.py look like for the above example?
Same as any other view. Usually you always use app_name.view_name as view parameter. Check Django Docs for urls
When I enter a submission on the forum and hit enter, I am brought to a blank screen. The error I recieve on the console is: Method Not Allowed (POST): / [19/Mar/2017 00:19:45] "POST / HTTP/1.1" 405 0
|
0
 I used this solution in my view.py file :
def view_name(request):
  if request.method == "POST":
    screenname = request.POST.get("handle", None)  # handle is the name of the input in the question.
    # Here you can do anything with your screenname, like passing it to the function.
  return render(request, 'path/to/form.html', {})

but getting the unicode error as "'unicode' object has no attribute 'script1_'"

FYI below is my code:

<form id="modifySingleTag" name="ModifiedSingletag" method="post" >{% csrf_token %}
<div class="input-wrapper">
    <div class="title first">
        <span class="option">copy and paste the tag to modify here:</span>
        <textarea class="input_textarea" name="input_textarea1"></textarea>
    </div>
</div>
<div class="buttons">
 <button id="modifyButton" name="Modifytag" class="btn primary" type="submit" title="" value="Modify"> MODIFY</button>
</div>
</form>

View.py:

    if request.method == 'POST' and request.POST['Modifytag'] == 'Modify':
       script1_ = request.POST.get("input_textarea1", None)
       print "I am script1 from Views.py".script1_
       modify_tag(script_=script1_, dimension='320X568', os_='ANDROID')
       print "the script from single tag view". script1_
    return render(request, 'home/index.html')

1 Comment

You might consider adding a few explanatory sentences as this increases the value of your answer for other users. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.