0

I found a couple of resources online, but none that worked for me. Here is what I currently have:

In views.py:

def button(request):
 return render(request, 'home.html')
def display_text(request):
 return HttpResponse('TESTING', status=200)

In urls.py:

urlpatterns = [
 url(r'^$', views.button),
 url(r'^display_text', views.display_text, name='script'),
]

In home.html:

 <div class="row">
 <input type="text" class="form-control js-text" id="input-box" placeholder="Type something to begin..."/>
 <div class="col-md-12" style="text-align:center;">
 <button onclick="location.href='{% url 'script' %}'"></button> <hr>
 </div>
 </div>

What happens right now is that it displays the string on a new web page. What I want to do is populate my text-box with that string returned by my Python function, and display it on the current page. How can I do so? Thanks!

asked May 13, 2020 at 6:53

1 Answer 1

1

The easiest way to do this is to make use of the render function provided by Django. This function combines a context and a template and renders a HttpResponse. You can change the display_text function to the following:

def display_text(request):
 return render(request, 'home.html', {'string': 'TESTING'})
 #Im assuming home.html is the the page you want to render

Now you can make use of Django templates to show the data sent through the context like so:

<div>
 <p>{{string}}</p>
</div>
answered May 13, 2020 at 7:14
Sign up to request clarification or add additional context in comments.

2 Comments

When I do this, I am redirected to another page with '/display_text' at the end. How can I do it so I am not redirected to that page, nor do I refresh my page.
Ok, so if you do not want your page to reload, that is a whole different topic. What you should be making use of is Ajax. Ajax is for creating asynchronous web apps, which in other words it can make petitions and receive responses without reloading your web page. I recommend starting here link, it provides an awesome example and simple example of Ajax with Django.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.