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!
1 Answer 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>
2 Comments
Explore related questions
See similar questions with these tags.