1

i want to get a value in an input to a django views, this is not a form as i am not trying to submit any form, i just need the value that is in the input field

this is the html template

<input readonly name="ref_code" type="text" value="{{ request.user.profile.recommended_by.profile.code }}">

this is my django views

code = str(request.GET.get('ref_code'))
print("This is the code:" + code)

It keep printing This is the code: None

NOTE: i have also tried using request.POST.get("ref_code") it did not still work What might be the issue?

2
  • 1
    i want to get a value in an input to a django views, this is not a form as i am not trying to submit any form There are two ways to get the value in a form: 1) Have the form be submitted to Django. 2) Have JavaScript read the value and send it to Django. There's no other way to get it. Commented Jul 28, 2022 at 2:49
  • Thanks that was what I needed then, I later used a form thanks to @lig answer Commented Jun 23, 2024 at 20:06

1 Answer 1

3

You definitely need something like a form.

So, let's walk you step by step on how your code works.

First, there is a view which prepares the data to be rendered with the template. This view responds to the first request from the user's browser and returns the rendered template as a static html page to the user.

Second, user's browser displays received html page to the user.

Third, user enters something into the input field.

At this stage the data user has entered is still on their side in their browser.

At this point you have two options:

  1. Use a form to let user send the data to some view on the server which will then process the received data.
  2. Use JavaScript to capture the entered data and send it to the server using an AJAX request.

I guess, you would like to use the second option in this case.

I'd recommend to read a guide on AJAX basics. The Ajax guide on MDN is a good place to start.

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

1 Comment

Thank you for your detailed answer then, I definitely needed a form if I was going to get some data over to the backend. Or else I'd use jquery/ajax to grab the input values and send them to the server.

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.