1

I'm a newbie to Django and started learning it last week. My question is I have a web page which has an input text box and a submit button. I want to capture the input string entered in the next web page (redirecting page) which has been entered in the text box after pressing the submit button in Django.

This is how the initial web page looks like

I have tried the following:

views.py

#View for the initial page
def index(request):
    return render( request, 'index.html')

#View for the Redirecting page -- This is where I want to catch the text box input
def results(request):
    inp_value = request.GET.get('text_search', 'This is a default value')
    context = {'inp_value': inp_value}
    return render( request, 'results.html', context)

forms.py

from django import forms

class TextForm(forms.Form):
    text_search = forms.CharField(label='Text Search', max_length=100)

index.html

<form action="/searcher/results/" method="get">
        <label for="results">Enter a string: </label>
        <input id="results" type="text" name="results" value="{{ search_results }}">
        <input type="submit" value="submit">
    </form> 

Can anyone point out why I'm not able to get the value of the textbox?

Thanks in Advance

0

1 Answer 1

1

request.GET key is the name of the input. So, in your case, since you want the value of <input type="text" name="results"> you should get that value from request.GET.get('results').

However, there is also a {{ search_results }} value that it's not get rendered from your index view. Thus, it will always be null.

def results(request):
    inp_value = request.GET.get('results', 'This is a default value')
    context = {'inp_value': inp_value}
    return render( request, 'results.html', context)
Sign up to request clarification or add additional context in comments.

Comments

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.