-1

I have a python script which process text, i want to create webpages with django and then django call the python script to execute it.

Here an Example for my case:

There is an input text in the web page, when we click in the button, the script take the input text, process it and return the result in the web page.

How can i perform this task?

1
  • 3
    That sounds like a perfectly normal Django workflow. What have you tried and where are you having problems? Commented Aug 17, 2016 at 14:12

1 Answer 1

8
  1. Create a form with a text input, like this:

    <form action="{% url "hello" %}" method="post">
      {% csrf_token %}
      <input name="hiya">
      <input type="submit">
    </form>
    
  2. Route the hello URL in urls.py:

    from . import views
    
    urlpatterns = [
       # ...
       url(r'^blabla$', views.whatever, name='hello'),
    ]
    
  3. In your views.py file, create the needed view:

    def whatever(request):
        if request.method == 'POST':
            import subprocess
            output = subprocess.check_output(["script.py", "--", request.POST['hiya'])
            return HttpResponse(output, content_type='text/plain')
    

You probably do not want to do this however. It is much more efficient to call a library function that to create an entire new process, and much easier too in general. Django also has niceties that makes doing anything less than a trivial form nicer, I would recommend reading the Django tutorial to understand them.

Sign up to request clarification or add additional context in comments.

2 Comments

Canonical example of good answer to bad question. I hope the most important piece of advice, “read the documentation before doing it the wrong way” does not get overlooked.

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.