I have the following form in my forms.py :
class PaymentMethodForm(forms.Form):
def __init__(self, *args, **kwargs):
super(PaymentMethodForm, self).__init__(*args, **kwargs)
payment_choices = ['online payment', 'payment at delivery']
self.payment_method_choice = forms.ChoiceField(choices=payment_choices, widget=forms.RadioSelect)
now this is the page in an online shop where you select your payment method. as you see, we have two methods, one is payment at delivery time which means there should not be any payments in the website, and the other one is online payment. In the case of the user choosing online payment, I should submit a form via post that looks something like this and after that redirect the user to the action url :
<form id="Form2" method="post" Action="https://somepaymentsite.com/gateway.aspx"
>
<input type="hidden" name="invoiceNumber" value="<%= invoiceNumber %>"
/>
<input type="hidden" name="invoiceDate" value="<%= invoiceDate %>" />
<input type="hidden" name="amount" value="<%= amount %>" />
<input type="hidden" name="terminalCode" value="<%= terminalCode %>" />
<input type="hidden" name="merchantCode" value="<%= merchantCode %>" />
<input type="hidden" name="redirectAddress" value="<%= redirectAddress
%>" />
<input type="hidden" name="timeStamp" value="<%= timeStamp %>" />
<input type="hidden" name="action" value="<%= action %>" />
<input type="hidden" name="sign" value="<%= sign %>" />
<input type="submit" name="submit" value=""continue />
</form>
now what I have in mind for doing this is that instead of putting this html stuff in my template (html file) I will get the radio button choice the user has chosen and if they chose online payment, I will submit a form like the above in my views.py . the problem is, I do not know how to go about this. I googled things but I didn't find anything good on how to post a form in a view in django. Can anybody help me? thanks.
If I understood correctly, "submit form from the view" is the same as do POST request to https://somepaymentsite.com/gateway.aspx with data {'invoiceNumber': 12345, 'invoiceDate': '10.10.2015', ...}. You can use requests library in this case.
# in view
# if online payment then
r = request.post('https://somepaymentsite.com/gateway.aspx', data=payload)
# processing r (error handling or something else)
where payload is dictionary {'invoiceNumber': 12345, 'invoiceDate': '10.10.2015', ...} that you construct somehow.
Related
So, last few days I was trying to build a website where you input the post data then hit submit and then boom your post appears on the home page/post page.
So, I have created the form but the problem is that I don't know how to send HTML form data to SQLite database so it can be viewed by multiple users anytime.
<form class="posts" action="." method="post">
<h2>Title</h2>
<input type="text" name="title" placeholder="Title">
<h2>Description</h2>
<textarea input class="des" name="description"type="text" placeholder="Description"></textarea>
<h2>Image(Optional)</h2>
<input type="file" name="inpFile" id="inpFile" class="img-btn">
<div class="img-prev" id="imgPrev">
<img src="" alt="Image Preview" class="img-prev__img">
<span class="img-prev__def-text">Image Preview</span>
</div>
<input type="submit" value="Submit">
</form>
I think that you should take a look at some Python's web-frameworks like Flask or Django first, so that you can understand this subject a little clearer.
So, I've hacked this together from a few sources, so if I'm totally going about it the wrong way I welcome feed back. It also occurs to me that this is not possible, as it's probably a security check designed to prevent this behavior being used maliciously.
But anyway:
I have a form on our Django site where people can request to change the name of one of our items, which should automatically create a jira ticket. Here's the form:
<form target="_blank" action='http://issues.dowjones.net/secure/CreateIssueDetails!init.jspa' method='get' id='create_jira_ticket_form'>
<a id='close_name_change_form' class="close">×</a>
<label for="new_name">New name: </label>
<input id="new_name" type="text" name="new_name" value="{{item.name}}">
<input type="hidden" value="10517" name="pid">
<input type="hidden" value="3" name="issuetype">
<input type="hidden" value="5" name="priority">
<input type="hidden" value="Change name of {{item.name}} to " name="summary" id='summary'>
<input type="hidden" value="{{request.user}}" name="reporter">
<input type="hidden" value="user123" name="assignee">
<input type="hidden" value="" name="description" id="description">
<input id='name_change_submit' class="btn btn-primary btn-sm" type="submit" value="Create JIRA ticket">
</form>
Then I have a little JS to amend the fields with the new values:
$(document).ready(function(){
$('#create_jira_ticket_form').submit(function(){
var watchers = ' \[\~watcher1\] \[\watcher2\]';
var new_name = $('#new_name').val();
var summary = $('#summary').val();
$('#summary').val(summary + new_name);
$('#description').val(summary + new_name + watchers);
})
})
It comes very close to working, but the description field is escaped, leaving it looking like:
Change name of OLDNAME to NEWNAME %5B%7Ewatcher1t%5D %5B%7Ewatcher2%5D
Which is less than helpful. How can I keep it as is so I can add watchers?
This happens when your form encodes the fields and values in your form.
You can try this out by this simple snippet:
console.log($('form').serialize());
you should see something like
description=ejdd+%5B~watcher1%5D+%5Bwatcher2%5D
in order to prevent this you should change your method='get' to method='post'.
The encoding happens because it's apart of HTTP, read here why
You can also read the spec paragraph
17.13.3 Processing form data
My goal is to run python function when a user clicks on a button within a form on my web page, when the argument is taken from textarea HTML element.
The following html code is my form with button within and is part of django application.
<div id="contact_form" class="col_400 float_l">
<form id="demoForm" name="contact" >
<label for="text">Your Review:</label>
<textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
<div class="cleaner_h10"></div>
<input type="button" onclick="return Button1_onclick()" class="submit_btn float_l" name="submit" id="predictSentBtn" value="Predict" />
</form>
</div>
I looked on django.forms and django.forms.widgets, but still don't understand how to "link" between existing html elements and python objects.
This should help:
<div id="contact_form" class="col_400 float_l">
<form id="demoForm" name="contact" >
<label for="text">Your Review:</label>
<textarea id="needid" name="needid" rows="0" cols="0" class="required"></textarea>
<div class="cleaner_h10"></div>
<input type="button" onclick="return Button1_onclick()" class="submit_btn float_l" name="submit" id="predictSentBtn" value="Predict" />
</form>
def function(request):
if request.method = 'POST':
print request.POST['needid'] # print request.POST.get('needid')
Try it ;)
I'm having problems with processing custom form data...
<input type="text" name="client[]" value="client1" />
<input type="text" name="address[]" value="address1" />
<input type="text" name="post[]" value="post1" />
...
<input type="text" name="client[]" value="clientn" />
<input type="text" name="address[]" value="addressn" />
<input type="text" name="post[]" value="postn" />
... (this repeats a couple of times...)
If I do
request.POST.getlist('client[]')
request.POST.getlist('address[]')
request.POST.getlist('post[]')
I get
{u'client:[client1,client2,clientn,...]}
{u'address:[address1,address2,addressn,...]}
{u'post:[post1,post2,postn,...]}
But I need something like this
{
{0:{client1,address1,post1}}
{1:{client2,address2,post2}}
{2:{client3,address3,post3}}
...
}
So that I can save this data to the model. This is probably pretty basic but I'm having problems with it.
Thank you!
Firstly, please drop the [] in the field names. That's a PHP-ism that has no place in Django.
Secondly, if you want related items grouped together, you'll need to change your form. You need to give each field a separate name:
<input type="text" name="client_1" value="client1" />
<input type="text" name="address_1" value="address1" />
<input type="text" name="post_1" value="post1" />
...
<input type="text" name="client_n" value="clientn" />
<input type="text" name="address_n" value="addressn" />
<input type="text" name="post_n" value="postn" />
Now request.POST will contain a separate entry for each field, and you can iterate through:
for i in range(1, n+1):
client = request.POST['client_%s' % i]
address = request.POST['address_%s' % i]
post = request.POST['post_%s' % i]
... do something with these values ...
Now at this point, you probably want to look at model formsets, which can generate exactly this set of forms and create the relevant objects from the POST.
Is there any way for Pyramid to process HTML form input which looks like this:
<input type="text" name="someinput[]" value="" />
or even more usefully:
<input type="text" name="someinput[0][subelement1]" value="" />
<input type="text" name="someinput[0][subelement2]" value="" />
<input type="text" name="someinput[1][subelement1]" value="" />
<input type="text" name="someinput[1][subelement2]" value="" />
...and access that data easily (e.g. via a dict)?
Any help would be much appreciated!
EDIT: to make it clearer, what I need is the ability to have a form where a user can add as many 'instances' of a group of input elements, e.g. adding between 1 and n users, each containing a firstname, lastname, username (or something like that).
One solution would be to use peppercorn. Although it does not support the syntax you're looking for, it will let you send structured data to your Pyramid application through the use of forms. A more casual description exists too.