Django: how to get HTML input field value to django view? - python

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?

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:
Use a form to let user send the data to some view on the server which will then process the received data.
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.

Related

Proper way to handle updating a view for Flask SPA using AJAX

This is a high level architectural question because I do not have web development experience, and I haven't found any succinct answers online.
Say I have a Flask app that starts off has a very simple html page with a few form fields. You key in some data into the form fields, submit, and the submission triggers an AJAX call onclick which then posts the results to your flask route.
When it comes time to display the data, you pull it out of the requests object which was posted to the relevant view, and then render a template passing through your formatted data.
My question is... if you want to continually make updates to the same page, how does that work? For example maybe after the first post hits your view you show a chart on the page with information passed to that view from the route. If you then decide to get a new graph for a different set of inputs to the form data, since you aren't hitting a new route do you just basically reload the page whenever the user keys in new data to the form fields and submits? How does this work if you want subsequent data to pop up after the chart? Do you just have multiple divs hidden/chilling in the background that unhide whenever certain events occur?
you usually use jquery to populate a div something like
<form id="my_form"> ... </form><button id="my_button">Clicky</button>
<div id="info_div"></div>
<script>
$("#my_button").click(function(){
var data = $("#my_form").serialize()
$("#info_div").load("/url/of/bit/to/load?"+data)
})</script>

Remove URL Params after page loads in Google App Engine

First of all, I am very new to web programming in general. I've read lots of tutorials online and it's a little confusing since some of them are out-dated. So if I'm heading down a path that is fundamentally wrong, I'd love suggestions about The Right Way. Having said that, this is just a for-fun project, so if there is a simple (<10 lines) way to get what I want, I'd love to see quick results today even if long-term I end up fundamentally changing things.
So anyway, I'm using Google App Engine (Python 2.7) to do a simple calculation. I show a form, the user fills in values, clicks a button at the bottom, and some result text is displayed with the result of the calculation. This part is working fine.
For my own testing, it's annoying to always re-type the input values, and some of the fields aren't totally obvious to users, so I'd like to be able to send a link to someone off the form "http://buggyapp.appspot.com/calculation?input1=100&input2=200" and have it fill in those two parameters. This part works, too.
The problem is when the user loads that sort of link, changes one of the values in the form, and then clicks the button. Instead of using the (changed) form value, it uses the value in the URL. I'd like to fix that, and ideally I'd like the URL bar to not even show the URL Params after the page loads.
I'm not sure what info is necessary to help you find what I'm doing wrong.
Here's a section from the HTML, which is a Jinja2 template:
<form method="post">
<input value="{{ input1 }}" name="input1">
<input value="{{ input2 }}" name="input2">
<button class="submit" type="submit">Calculate</button>
</form>
<p>{{ result }}</p>
Here's the python code:
def get(self):
input1 = request.get('input1')
input2 = request.get('input2')
# some irrelevant(?) code to set default values on the initial load if there are no URL Params
result = str(input1+input2) #actual calculation slightly more complicated than this
template = JINJA_ENVIRONMENT.get_template('calculation.html')
self.response.write(template.render(vars()))
It seems to behave the same no matter whether I put the calculation in the get handler or post handler. At the moment, I'm simply calling one from the other:
def post(self):
input1 = request.get('input1')
print input1 # even this shows the URL value (if one exists), not form value
return self.get();
So, my next step to make it work would be to change the names of the form inputs so they are different from the URL Params, and add extra code to merge the potential input sources. But that won't really get me what I want, which is for the URL Params to disappear once they have populated the input forms. It also complicates the part of the code that generates those sorts of links.
I'd prefer a Python-only solution, but I'm open to using javascript. There are other stack overflow questions/answers that imply a javascript mechanism will do what I want, but I don't understand any of them well enough to apply them to my problem.
I'm sure I'm doing lots of other things wrong, and I may have over-simplified things or not posted enough info. I'm here to learn, so fire away. And thanks in advance for the help.
tl;dr- How do I use the URL Params once and then remove them from the URL so they don't override form inputs on subsequent posts?
HTTP GET requests usually include the parameters in the URL since there's no other place to pass them (there's no request body).
HTTP POST requests usually include the parameters in the request body, though it's still possible to include the parameters in the URL.
It looks like you're using some sort of Javascript to post the form. Make sure you are using a POST request and putting the parameters in the POST body. Most libraries automatically put params in the POST body as long as you're issuing a POST request.
EDIT:
Forms usually issue POST requests by default. Usually the <form> element will have an action attribute that specifies the URL to send to. However, if it doesn't have an action attribute it'll issue a POST request to the current URL.
In your case the current URL contains parameters and those are submitted again with your request. You should have a few options.
Specify the action in your form so you aren't submitting to the same url with parameters already attached.
In your request handler, read the data out of the post body (request.POST['input1']) instead of the url.

Better way of passing form parameters into hidden form in Pyramid

In a previous question, I was trying to figure out the right strategy for to passing data between forms in Pyramid. Based on the answer I received, I decided the approach of using a hidden form.
I started implementing this and think there must be a better way of passing along the data. Specifically, passing parameters through the url results in a tuple that is messy to parse.
I want it to be general enough to not to know what parameters the form has and also it needs to handle file fields as well.
How I'm currently attempting to pass the form data to the confirmation page:
#view_config(renderer="templates/derived/load/error.mak", route_name='process_model_route')
def process_model(self):
#processing logic and validaton, failiure in validation sends user to error.mak
return HTTPFound(route_url('confirm_model_route', self.request, fparams=self.request.POST))
Route: config.add_route('confirm_model_route', 'rnd2/model/confirm/*fparams')
#view_config(renderer="templates/derived/confirm/model.mak", route_name='confirm_model_route')
def confirm_model(self):
form_dict = self.request.matchdict['fparams']
#need to decode and pass to template
return dict({'load_route':load_route, 'form_dict':form_dict})
The confirm/model.mak template would contain the hidden form.
The idea with this method is:
Client visits page.
Server renders the form.
Client fills in form and POSTs to URL.
Server renders a new page that contains a hidden form with all of the data it just received in the POST.
Client POSTs to a URL, confirming the submission.
Server persists the data from the hidden form and redirects.
Now depending on usability, it's up to you to decide how many different URLs you actually want here and how many views in Pyramid. You have to think about what happens with invalid data?
Notice in the outline above, once the user POSTs the form to a URL, that URL must return the confirmation page containing a hidden form. If you try to redirect the user to a confirmation page instead, you must persist the data somehow, either in a session or through the hack you showed in your example (shoving all of the data into the GET). The second solution is very bad because it abuses the true purpose of GET in HTTP.
There is also the convention that every POST should result in a redirect to avoid a client submitting the form multiple times. With this in mind you might consider the simple solution of rejecting POSTs that do not have a "confirmed" flag and simply setting the "confirmed" flag in javascript after prompting the user. This allows you to keep your form handling logic simple.
If you don't want to rely on javascript and you don't want to persist the form data in a session, then you run into the issue of not redirecting after the first POST but other than that it should be simple from the outline above.

Getting the name of a checkbox submitted by GET

So I'm trying to use the CGI module that comes with Python, on a Python Server Page.
I want to know if there is any way to pull the name of the checkbox, when it is submitted. I don't actually need to know the status of whether it is on or off (because off won't submit to the new page), just what the name is, because that's how the script is going to run.
I.e., on my first page I have
<input type="checkbox" name="AV-D01">
I submit this form, and it has the form of
formchangedate.psp?AV-D01=on
All I care to pull from this is the AV-D01 - is there anyway to do so?
cgi.FieldStorage() returns a dictionary containing the parameters of the request.

Pass variables to a "success" page after processing a form in Django

Is there anyway to pass context variables to a redirect response? I want to redirect a user to a success page after they submit a form, but I don't want the success page to be just a static html file. I need to display extra information based on the form data.
I have looked at this question, but the solution presented there simply renders a different file at the same url. I'd like to redirect the user so that hitting refresh at the page won't submit duplicate entries into the application.
Right now the only thing I have been able to use with some success is redirecting to a url while passing it GET variables as described here. That just seems like a bit of a hack, and was just wondering if there is any better solution...
Thank You
The way I see it you have three options:
Use GET variables in the redirect.
Store something in the session.
If you are creating an object using the form that was submitted, put the id of that object in the redirect url and use it in the new view.
The limitation you are running up against is that http is stateless, not something inherent in django.
How about storing your values in a session, then have the redirected page pick up the values from there?

Categories