0

At first , I would like to take an integer input from user . After getting the input , I would like to go to the url based on what the user has entered . However , I am not able to implement it . Here's my urls.py file:

urls.py

urlpatterns = [
    path('',views.page1),
    re_path(r'^form/(?P<data>[1-4]{1})',views.page2),
]

Here's my views.py file:

views.py

from django.shortcuts import render
from .forms import CustomForm1
from django.http import HttpResponse
import requests
# Create your views here.
def page1(request):
    if request.method == 'POST':
        form = CutsomForm1(request.POST)
        if form.is_valid():
            data = form.cleaned_data['data']
    else:
        form = CustomForm1
        data = 0
    return render(request,'homepage/base.html',{'form':form,'data':data})

This is my forms.py file:

forms.py

from django import forms

class CustomForm1(forms.Form):
    data = forms.IntegerField(label='data',required=True)

And here's the HTMl:

HTML

<form method="POST" action="form/{{ data }}">
            {% csrf_token %}
            {{ form.as_p }}
            <ul class="actions">
                <li><input type="submit" value="Send Message" class="primary" /></li>
                <li><input type="reset" value="Clear" /></li>
            </ul>
        </form>

Initially , when the form is loaded , my data variable has value 0 , but I want to it get the value which is entered by user , hence , I can then route to the desired url as per the updated data variable which is passed as context .

For example , in the input field ,if the user enters 1 , I want it to go to form/1 but i feel that the django templating is static and is not updateable once passed.Any other way I can implement it?

2
  • 1
    django is a server-side framework. It doesn't work on the client-side(browser). You may use javascript to capture the user input and redirect it accordingly. Commented Dec 9, 2019 at 19:28
  • Oh! Will try it out using JavaScript. Commented Dec 9, 2019 at 19:41

1 Answer 1

1

In views.py :

from django.urls import reverse
from django.urls import redirect

def page1(request):
    if request.method == 'POST':
        form = CustomForm1(request.POST)
        if form.is_valid():
            data = form.cleaned_data['data']
            return redirect(reverse('form_url', kwargs = {'data' : data}))
    else:
        form = CustomForm1
        data = 0
    return render(request,'homepage/base.html',{'form':form,'data':data})

urls.py:

urlpatterns = [
    path('',views.page1),
    re_path(r'^form/(?P<data>[1-4]{1})',views.page2, name = 'form_url')
]

Basically I cleaned 1 from the form and used reverse to create the url and I redirected to it. Read about reverse : https://docs.djangoproject.com/en/dev/ref/urlresolvers/#reverse

[EDIT] as @mihai mentioned in the comments, remove the action="form/{{ data }}" if you want this answer to work.

Sign up to request clarification or add additional context in comments.

4 Comments

The call to redirect can be simplified as in the docs: redirect('form_url', data=data).
I tried reverse out but it is still redirecting me to form/0 . Also , tried out redirectas the above comment states but I am still not able to do it. redirect loads the same page but this time with different data right?
You need to also remove the action="/form/{{ data }}/" in your template if you want to use this answer. You should probably clarify in your question what you are trying to achieve. From what I gather, page1 has a form where you select a number from 1 to 4, and when you submit the form, you go to the page2 view (which btw you haven't included in your question) corresponding to that number. If so, then omit the action attribute of the form to send data to the same page1 view where the form is initially rendered, since that's where the code handling the POST is located.
Yes. I want to first take an input from the first form (any number from 1 to 4) and then after filling the form , I want to route the url to form/1 or form/2 likewise depending upon the input entered . Also tried out what you suggested and it worked! Thank you very much!

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.