0

I have a simple form which shall take a user input and directly send the user to a dynamic url using the input. Inputing "111" into the form shall send the user to http://127.0.0:8000/number_input=111

Somehow i can not get it done, since i do not know how to get direct variable value from the form to the view immediately. I can build it with 2 views but that looks somehow ugly and inconvenient.

Here is what i have so far:

The Form: (within index.html)

<form action={% url 'number_view'%} method="POST">
            {% csrf_token %}
            <p><input type="text" name="inp_number" placeholder="Insert Number"/></p>
            <p><input type="submit" value="Submit"/></p>            
        </form>

views.py:

def number_view(request):
    if request.method == 'POST':
        context = {'number': request.POST['inp_number']}
    return render(request, 'number.html', context)

urls.py

from . import views

urlpatterns = [  
    path('number_input=<int:number>/', views.number_view, name='number_view')
]

The dynamic part here (int:number) will make the code fail, since iam not sending a value from the form, right? But i only know how to send static values (i.e. action={% url 'my_view' 111 %}. How can i send direct dynamic values from the form itself (the value that the user inputs in the submit button)?

number.html

<h2>This page is for number {{ number }}</h2>

2 Answers 2

1

You can redirect the user to the correct URL using redirect.

from django.shortcuts import redirect


def number_view(request):
    if request.method == 'POST':
        sent_number = request.POST['inp_number']
        return redirect('number_view', number=sent_number)

    return render(request, 'number.html', context)

For example, if you will handle the number only when it is posted:

from django.shortcuts import redirect


def number_view(request):
    number = None
    if request.method == 'POST':
        number = request.POST['inp_number']

    return render(request, 'number.html', {'number': number})


urlpatterns = [  
    path('number_input/', number_view, name='number_view')
]
Sign up to request clarification or add additional context in comments.

11 Comments

that still causes a "Reverse for 'number_view' with no arguments not found. 1 pattern(s) tried: ['number_input=(?P<number>[0-9]+)/\\Z']". Somehow it must be possible to send dynamic data in my form, no?!
Debug and check if you receive any form data, especialöy for inp_number. My example does not include any error handling.
the thing is, i want to know how i can send dynamic data from the form to the view, in order to direct the user immediately to a dynamic url. This should be possible no? Without redirecting the user from within the view. Your solution does not provide for that because it does not take care for the dynamic part of the "number_input=<int:number>/" url. PS: Yes i receive data for request.POST, but you do not even get to ''number_view' function because of the Reverse exception stated in above comment (because no arguement is given from the html form itself)
It is what are you asking for. And sure this is possible to send form data to a view, this is how forms work. I will add another example in my answer since I don't understand what you will achieve with the <int:number> in your URL configuration.
It should be clear to post to your number_view <form action={% url 'number_view'%} method="POST">. We have no idea what my_view should be as long you do not provide enough code.
|
0

This should work:

from django.shortcuts import redirect


def number_view(request, number):
    if request.method == 'POST':
        number = int(request.POST('inp_number'))
        return redirect('url', number=number)

return render(request, 'number.html', {'number': number})

urls.py

urlpatterns = [
    path('number_input=<int:number>/', views.number_view, name='number_view')
]

and your form:

<form method='POST'>
    {% csrf_token %}
    <p><input type="text" name="inp_number" placeholder="Insert Number"/> 
    </p>
    <p><input type="submit" value="Submit"/></p>            
</form>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.