In bootstrap form, how to connect "submit" button to database? - twitter-bootstrap-3

I recently started to use this bootstrap_form from django_bootstrap3 package. It's a nightmare... Here is a simplified version of what I want to do:
- I have 2 textboxes for user to enter data
- POST the data to a local database through clicking a "submit" button
What I had originally was to have a base_form.html file that store the form such as:
{% extends "expense/base.html" %}
{% block content %}
<form method="post">
{% csrf_token %}
<input id="field1" type="text" name="field1" value="{{ field1 }}">
<input id="field2" type="number" name="field2" value="{{ field2 }}">
<input id="submit" type="submit" value="Submit">
</form>
Then I would just call this form in my base.html, everything works, just ugly. Now that I started to use django_bootstrap3 package, it's great looking, less code, just nothing works. With django_bootstrap3, I no longer need the base_form.html (it grabs data from forms.py directly), instead I just need to call {% bootstrap_form form %} like the following:
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary btn-lg btn-block" data-loading-text="Loading...">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
However, there is no place for me to say this is a form POST not GET. Also, there is no place for me to say these are all input field, and submit button should actually submit. Right now the behavior is that after I write some data to the field, and click submit button, nothing is posting to the database. The submit button doesn't work at all.
I don't know what I am missing here. I am very new to web development using django and bootstrap framework. Please help.

Related

when i click on add to cart button, page refreshes and then product is added how can i do this without getting the page refreshing

i added the button on product page below each product but they are refreshing the page i tried this code but whenever i click on add to cart page refreshes, how can it be done without refreshing the page and product still get added to cart without refreshing and redirecting to other page
<div class="buy-now-bx">
{% if card_product.available %}
<form method="post" action="/cart/add">
<input type="hidden" name="id" value="{{ card_product.selected_or_first_available_variant.id }}">
<div style="display: flex">
<input type="hidden" name="return_to" value="back" />
<input type = "submit" value = "Add to Cart" name = "addbutn" id="addbutn" class="button buynow-btn" data-id="{{ card_product.variants.first.id }}"/>
</div>
</form>
{% else %}
<a class="button buynow-btn" tabindex="0" href="{{ card_product.url | default: '#' }}">Sold out</a>
{%- endif -%}
</div>
To add an add-to-cart button to a Shopify product page without causing a page refresh, you can use Shopify's AJAX API to perform an AJAX request to the Shopify cart API. This allows you to add products to the cart without a page refresh.
Here's an example code snippet that you can use to achieve this:
In your product.liquid file, add a button to trigger the AJAX request when clicked:
<div class="buy-now-bx">
{% if card_product.available %}
<form method="post" action="/cart/add" id="add-to-cart-form">
<input type="hidden" name="id" value="{{ card_product.selected_or_first_available_variant.id }}">
<div style="display: flex">
<input type="hidden" name="return_to" value="back" />
<button type="submit" class="button buynow-btn" data-id="{{ card_product.variants.first.id }}" id="add-to-cart-btn">Add to Cart</button>
</div>
</form>
{% else %}
<a class="button buynow-btn" tabindex="0" href="{{ card_product.url | default: '#' }}">Sold out</a>
{% endif %}
</div>
Add the following code to your theme.js file or create a new file with the following code:
$(document).ready(function() {
$('#add-to-cart-form').submit(function(event) {
event.preventDefault(); // prevent the default form submit behavior
var form = $(this);
var addToCartButton = form.find('#add-to-cart-btn');
var originalButtonText = addToCartButton.text();
addToCartButton.attr('disabled', true).text('Adding...'); // disable the button and change the text
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: form.serialize(),
dataType: 'json',
success: function() {
addToCartButton.text('Added!'); // change the button text
setTimeout(function() {
addToCartButton.attr('disabled', false).text(originalButtonText); // re-enable the button and change the text back to the original text after a delay
}, 1000);
},
error: function() {
addToCartButton.text('Error'); // handle the error by changing the button text
}
});
});
});
This code will handle the form submit event and send an AJAX request to the Shopify cart API when the add-to-cart button is clicked. It will also disable the button and change the text to indicate that the product is being added to the cart. Once the request is complete, it will change the button text to "Added!" and then change it back to the original text after a delay.

how to add customer name in shopify coming soon page?

I am looking for a simple way to implement a simple "coming soon" (pre-launch) page for my project. Users should be able to leave an email and name in order to be notified when the project is launched.
You already have an email field on this page. When enters his email and clicks the Notify button, an inactive customer account will be created for that user. On launching your store you can send account invites to all the customers that submitted this form, using standard Shopify functionality.
You mentioned that you also want to collect customers names. You can do that by adding additional fields to this form. You're using Debut theme so just open the sections/password-content.liquid file and add these fields between {% form 'customer' %} ... {% endform %} tags. Note, as Shopify customer will be created, you have to use two fields - one for the first name and one for the second name. Just duplicate the email field and change name attributes. See an example below how these fields may look like, note how field names are grouped with contact[...]:
<input type="text" name="contact[first_name]" placeholder="First Name">
<input type="text" name="contact[last_name]" placeholder="Last Name">
You can also change the tags to be applied to the customer on creation. In the Debut theme, these tags are password page and prospect by default.
Adding more fields
You can collect more information on this page. Just add "note" fields with names like contact[note][Field name]. The information from these fields will be displayed in the Customer Note field. For example, if you want to ask customer leave a phone number you would use something like that:
<input type="text" name="contact[note][Phone]" placeholder="Phone">
You can follow the logic from this tutorial: Add fields to the customer registration form. Just make sure you're grouping fields with contact[] prefix rather than customer[] as described in the tutorial, which is actually about another form.
What I found with the Debut theme is that if you put
<input type="text" name="contact[first_name]" placeholder="First Name">
<input type="text" name="contact[last_name]" placeholder="Last Name">
in between these tags {% form 'customer' %} ... {% endform %}
The form fields get squished together.
I found that the below worked for me, if you add it before the section {% form 'customer' %} ... {% endform %}:
<center><input
type="text"
name="contact[first_name]"
placeholder="First Name">
<input
type="text"
name="contact[last_name]"
placeholder="Last Name"
></center>
<br>
I've added a screenshot to show you what it looks like in Shopify
This is the final result of my code
I have got it working to capture the first and second name by adding these fields:
<label>First Name</label>
<input
type="text"
name="contact[first_name]"
id="{{ formId }}-first_name"
class="input-group__field {% if form.errors contains 'first_name' %} input--error{% endif %}"
placeholder="Please enter your first name..."
{%- if form.errors contains 'first_name' -%}
aria-invalid="true"
aria-describedby="{{ formId }}-first_name-error"
data-form-status
{%- endif -%}
>
<label>Last Name</label>
<input
type="text"
name="contact[last_name]"
id="{{ formId }}-last_name"
class="input-group__field {% if form.errors contains 'last_name' %} input--error{% endif %}"
placeholder="Please enter your surname..."
{%- if form.errors contains 'last_name' -%}
aria-invalid="true"
aria-describedby="{{ formId }}-last_name-error"
data-form-status
{%- endif -%}
>
I'd love to know how to display errors if the fields are left blank though. Anyone know how to do this?
Many thanks.

Adding an "ADD TO CART" button to a collection

I have a client asking to add an "ADD TO CART" button under each product in their collections and I can't figure out how.
I'm new to the Shopify platform and I am not familiar with Liquid but I did try to use the built in "Channel" of adding a Buy Button, but when trying to paste in the code it didn't populate anything in the preview page.
Any help would be wonderful.
Use the shopify API by adding this into the collection.liquid
<form method="post" action="/cart/add">
<input type="hidden" name="id" value="{{ product.variant.id }}" />
<input min="1" type="number" id="quantity" name="quantity" value="1"/>
<input type="submit" value="Buy" class="btn" />
</form>
Add that somewhere in this loop
{% for product in collection.products %}
...
{% endfor %}
product.variant.id identifies which item is added to the cart and it could be replaced with product.variants.first.id
This details how it works and applies to standard, non ajax, forms too
https://help.shopify.com/en/themes/development/getting-started/using-ajax-api#add-to-cart

What's the simplest way to add additional HTML in Django forms and override form layout?

I have a form set up using Django's form facilities and it gets rendered in the template like so:
{% block content %}
<h1>Register to join Forge Design</h1>
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
I have the fields First name, Last name, Email and Available date, which will render out my fields vertically with their labels left aligned. What I would like is to control each field such that I will have a layout like in the screenshot below:
Desired form layout
Could anyone best advise how to customise so that I can intersperse labels in between fields such as the "Name" and "Email Address *" and change the positioning of the first and last name labels so that they sit below their respective fields. Is this something I must do by replacing in the template {{form.as_p}} with the individual {{form fields}} and then wrapping that in a div to be controlled by CSS?
Thanks
You can access to each field by name define in your form
{{ form.first_name.errors }} # Error of your field first name
{{ form.first_name.label_tag }} # Label of your field first name, generate a label tag
{{ form.first_name }} # Input for your field first name
https://docs.djangoproject.com/en/1.10/topics/forms/#rendering-fields-manually

Flask: how to link wtforms with jinja2-templates using bootstrap

it seems a stupid question but I do not really get the connection.
I use tghe latest bootstrap version ad have this template:
<div class="form-group"{% if form.name.errors %} error{% endif %}>
<label for="name" class="col-md-2 control-label">Name:</label>
<div class="col-md-10">
<input type="text" class="form-control" id="name" placeholder="Enter your name here">
{% for error in form.name.errors %}
<span class="help-inline">[{{ error }}]</span><br>
{% endfor %}
</div>
</div>
The field is rendered as desired but no matter what I enter there my form will raise the error, that this field is required.
Please let me know what I miss here - how do I link the field in the template to my form properly?
The issue was that the "name" attribute was missing - only having the "id" is insufficient.