I am working in a django application where braintree is used for payment purpose. The braintree version I am using is 3.13.0. I am using the braintree.js dropin-ui alongside the braintree app.
I tried to add a payment method using a custom form with fields 'cardholder_name','number' and 'expiration_date'. When I click on the 'Add Payment Method' link, a server-side request is sent to get the client token. Using that I set up braintree as given below :
function setup_braintree(){
$.ajax({
url:'/get_token/',
type: 'GET',
datatype:'json',
data: {
payment_freq: 1,
},
success: function(d) {
if (d !== null){
$('#dropin-container').html('');
braintree.setup(d.client_token, "dropin", {
container: "dropin-container"
});
}
}
});}
My form is as given below:
<form name="PaymentMethodForm" id="PaymentMethodForm" action="/payment_method/add/" method="POST">{% csrf_token %}
<div id="dropin-container" style="display:none;">Loading...</div>
<div class="form-group">
<label for="id_name_on_card">Name On Card:</label>
<input type="text" id="id_name_on_card" name="name_on_card" value="" class="form-control"/>
</div>
<div class="form-group">
<label for="id_card_number">Card Number:</label>
<input type="text" id="id_card_number" name="card_number" value="" class="form-control"/>
</div>
<div class="form-group">
<label for="id_expiration_date">Expiration Date:</label>
<input type="text" id="id_expiration_date" name="expiration_date" value=""class="form-control"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="submit" class="btn btn-default">Save</button>
<input type="submit" class="cool-fc button swagbutton" value="Submit" id="submitbutton" name="submitbutton" style="display:none">
</div>
</form>
As per the documentation, a 'hidden' field 'payment_method_nonce' is created, but its value is null.
When I submit the form, 'nonce is required' error is being returned.
What can be the issue here? My server code is given below:
result = braintree.PaymentMethod.create({
"customer_id": request.user.username,
"payment_method_nonce": request.POST['payment_method_nonce'],
"number":request.POST['card_number'],
"cardholder_name":request.POST['name_on_card'],
"expiration_date":request.POST["expiration_date"],
"options":{
"verify_card":True
}
})
Also is there option in dropin-ui to 'add payment method' without using custom form?
Thanks in advance.
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
For the Drop-in, you don't need to include any input tags, braintree.js will render an iframe with the form inputs inside the div specified in braintree.setup(...). Then when the user submits this form, the payment method nonce should be available to you server-side.
Your form should simply be
<form name="PaymentMethodForm" id="PaymentMethodForm" action="/payment_method/add/" method="POST">{% csrf_token %}
<div id="dropin-container"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="submit" class="btn btn-default">Save</button>
<input type="submit" class="cool-fc button swagbutton" value="Submit" id="submitbutton" name="submitbutton" style="display:none">
</div>
</form>
If you would like more control over the styling still have the easiest level of PCI compliance, I would take look at Hosted Fields.
Hope this helps!
Related
{% csrf_token %}
User name
</div>
<div class="form-group">
<label class="form-label" for="form2Example2">Password</label>
<input type="password" id="form2Example2" class="form-control" name="password" required/>
</div>
<button type="button" class="btn btn-primary btn-block mb-4">Sign in</button>
i try to post data like email id and password in post method , but it sent through get method . I think there may be problem with bootstrap. can u tell me what are the modifications to be done in the bootstrap for post data method.
thanks
If you want to send it as post, then use button type: "submit". Also you might need form section, if you don't have it. Like this:
<form method="POST">
{% csrf_token %}
<div class="form-group">
(...)
</div>
<button type="submit" class="btn btn-primary btn-block mb-4">Sign in</button>
</form>
You need to add method = "POST" with {% csrf_token %} and submit button
<form method="POST">
{% csrf_token %}
<div class="form-group">
<label>Email address</label>
<input type="email" class="form-control">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Signin</button>
</form>
I'm new to ajax and I need help. I was able to open the modal when I clicked the Add grade, now my problem is how to save the form to the database using modal? that error page will show if I hit the save changes in modal form.
views.py
class GradeUpdateView(LoginRequiredMixin, UpdateView):
model = Subject
template_name = 'teacher/modal.html'
# fields = ['grade']
# success_url = '/'
form_class = UpdateGradeForm
def get_success_url(self):
subject = self.object.subject_name
year = self.object.year_taken
return reverse_lazy('student-list', kwargs={"subject_name_id": subject.id, "year_taken": year})
modal.html
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form method="post" id="grade-form">
<div class="modal-body">
{% csrf_token %}
{{ form.as_p }}
{% comment %} <input type="submit" class="btn btn-sm btn-block btn-primary" value="Save"> {% endcomment %}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" data-form-url="" class="btn btn-primary" id="save-form">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
main.js
var modalDiv = $("#modal-div");
$(".open-modal").on("click", function () {
$.ajax({
type: 'GET',
url: $(this).attr("data-url"),
success: function (data) {
modalDiv.html(data);
$("#exampleModalCenter").modal();
}
});
});
<form method="post" id="grade-form" action = " The url path where you will handle the submit of form">
<div class="modal-body">
{% csrf_token %}
{{ form.as_p }}
{% comment %} <input type="submit" class="btn btn-sm btn-block btn-primary" value="Save"> {% endcomment %}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" data-form-url="" class="btn btn-primary" id="save-form">Save changes</button>
</div>
</div>
</form>
just add an attribute action in the following form when you click on submit it will take you to the url equivalent to href url hope you understand
I am working on a job portal system. I provide users the chance to edit their profiles by adding and removing job experiences. The issue is I don't know how to send that data to the view so that it can retrieve all the fields and store them in the database accordingly. By retrieving, I mean for each job experience, retrieve their corresponding sub fields
Here is my template file:
<!--JOB EXPERIENCE-->
<div class="job_experience">
<h5>Experience</h5>
<hr>
<!--FORM-->
<form action="#" id="save_job_experience_form" method="POST">
{% csrf_token %}
<div class="job_container">
{% for job in job_experience %}
<div class="exp">
<label>Organisation</label>
<input type="text" class="form-control" name="organisation
placeholder="Organisation" value="{% if job.organisation %}
{{job.organisation}}{% endif %}" required>
<label>Job Title</label>
<input type="text" name="job_title" class="form-control" value="{% if job.job_title
%}{{ job.job_title }}{% endif %}" placeholder="Job Title e.g Backend Developer"
required>
<button style="margin-left:15px;" type="button" class="btn btn-danger
remove_job_exp"><strong>Delete Job Experience</strong></button>
</div>
<hr>
{% endfor %}
</div>
<!--BUTTONS-->
<input type="submit"value="Save">
<input type="button" id="add_job_experience"value="Add one">
</form>
</div>
<script>
$(document).on('click', '#add_job_experience',function(e){
e.preventDefault();
$(".job_container").append(`<div class="exp">
<!--IT GOES ON AS ABOVE -->
$(document).on('click', 'button.remove_job_exp', function(e){
e.preventDefault();
$(this).closest('div.exp').remove();
});
You can post multiple inputs with the same name like so:
<input type="text" name="job[]" value="job1"/>
<input type="text" name="org[]" value="org1"/>
<input type="text" name="job[]" value="job2"/>
<input type="text" name="org[]" value="org2"/>
Then in django you can parse the POST data like so:
#require_POST #require POST method or check if request method is POST
def add_jobs(request):
jobs = request.POST.getlist('job[]')
orgs = request.POST.getlist('org[]')
...
Or use Django Formsets: https://docs.djangoproject.com/en/3.2/topics/forms/formsets/
I'm trying to send the value of a button when a form is submitted using the POST method.
{% block content %}
<div class="modal-dialog modal-lg" role="document">
<form action="" method="post" id="news-create" class="form">{% csrf_token %}
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title">Add News</h4>
</div>
<div class="modal-body">
{{ form }}
</div>
<div class="modal-footer">
<input class="btn btn-primary" type="submit" name="save" value="Save" />
<input class="btn btn-primary" type="submit" name="save_new" value="Save as new" />
</div>
</div><!-- /.modal-content -->
</form>
</div><!-- /.modal-dialog -->
<script>
var form_options = { target: '#modal', success: function(response) {} };
$('#news-create').ajaxForm(form_options);
</script>
{% endblock content %}
But when I print the QueryDict request.POST inside the file view.py the value of the button is not present.
How can I get the value? I need to perform different action based on the button clicked.
At the moment I'm using a bootstrap modal view to render a form (UpdateView) based on a model.
Thanks for the support
To send the value of the submit button the name attribute of that button should be "submit".
So try it like this:
<input class="btn btn-primary" type="submit" name="submit" value="Save" />
<input class="btn btn-primary" type="submit" name="submit" value="Save as new" />
Then you can do request.POST.get('submit') on your views to get the value of the button which was clicked.
You can also use checkbox for this kinds of things.
I want to upload a csv file to the database using Django rest api.
My content of html page
<form class="form-inline " method="POST" action="uploadfile" enctype="multipart/form-data">
<div class="form-group col-lg-3 col-sm-5">
<label for="selectclient">SELECT CLIENT : </label>
<select class="form-control ml-2 w-50" id="selectclient">
<option>Client</option>
<option>USPLIMARA</option>
<option>USPLMSTAKEN</option>
<option>USPLWROGN</option>
</select>
</div>
<div class="form-group col-lg-3 col-sm-3">
<label for="selecttype">Type : </label>
<select class="form-control ml-2 w-50" id="selecttype">
<option>Type</option>
<option>SALES</option>
<option>INVENTORY</option>
<option>RETURNS</option>
</select>
</div>
<div class="form-group col-lg-3 col-sm-4 ">
<label for="selectdburl">DBURL : </label>
<select class="form-control w-50 ml-2" id="selectdburl" name="dburl">
<option>Dburl</option>
<option>localhost:3306/usplimara</option>
<option>localhost:3306/usplmstaken</option>
<option>localhost:3306/usplwrogn</option>
</select>
</div>
<div class="form-group col-lg-2 col-sm-4 ">
<input type="file" id="uploadfile1" name="missingcsvfile" class="mt-2">
<button type="submit" class="btn btn-outline-primary btn-sm mt-2">Upload file</button>
</div>
</form>
I created a Rest Api
urlpatterns = [url(r'^upload/(?P<filename>[^/]+)$', views.FileUploadView.as_view()),]
I don't know how to call the api to access this Api or how to specify it in the action method. Please let me know.
Action attribute tells where the data from the form should be send. One tricky thing, is that you have an URL parameter with a name of the file defined in the URL configuration. So it'd have to be different for every new file. If it has to stay this way, you'd have to use JavaScript to dynamically override action attribute based on the name of the file.
Another solution would be to change your URL configuration so you can upload using /upload without the need of specifying the name in the URL (you can always send the name as a part of the form data)