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>
Ougaga
  • 127
  • 1
  • 9

2 Answers2

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')
]
Marco
  • 2,371
  • 2
  • 12
  • 19
  • that still causes a "Reverse for 'number_view' with no arguments not found. 1 pattern(s) tried: ['number_input=(?P[0-9]+)/\\Z']". Somehow it must be possible to send dynamic data in my form, no?! – Ougaga Sep 05 '22 at 19:18
  • Debug and check if you receive any form data, especialöy for `inp_number`. My example does not include any error handling. – Marco Sep 05 '22 at 19:37
  • 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=/" 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) – Ougaga Sep 05 '22 at 20:22
  • 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 `` in your URL configuration. – Marco Sep 05 '22 at 20:48
  • please try the provided example. You do not get to number_view because you do not deliver any argument to the dynamic part specified in urls.py, i.e. . And that is what i am specifically asking about, meaning how to deliver that part dynamically from within the form? – Ougaga Sep 05 '22 at 20:54
  • 1
    It should be clear to post to your number_view `
    `. We have no idea what `my_view` should be as long you do not provide enough code.
    – Marco Sep 05 '22 at 20:59
  • err, i'm sorry, this was wrong standardized from copy/pasting. My_view is number_view naturally. I've edited the original post – Ougaga Sep 06 '22 at 07:17
  • right now your solution works, yes, but this is for a static url http://127.0.0.1:9000/number_input. What i want to achieve is the dynamic part http://127.0.0.1:9000/number_input=**** which i am struggling with where **** shall somehow be transmited through the html form itself. So that in the end i will end up with something like http://127.0.0.1:9000/number_input=5555, when the user inputs "5555" into the html form and hits "submit". Also this kind of url can be altered dynamically via your browser by just altering the numbers inside the url. – Ougaga Sep 06 '22 at 07:45
  • I think using the equal sign in an URL except for declaring parameters should not be used! Better use a GET parameter or a separate detailview to handle ids like `...000/number_input/5555/` – Marco Sep 06 '22 at 08:26
  • is that the best practice? I read that this kind of communication between client/server is to unsafe for that – Ougaga Sep 06 '22 at 08:27
  • As long as we don't understand your full use case we are not able to tell you which solution you should pick to handle form data. But I think you should open a new question for this as it is not related to this post. – Marco Sep 06 '22 at 08:55
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>
  • 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](/help/how-to-answer). – Community May 13 '23 at 08:48