XpertCoder
XpertCoder

Reputation: 35

Not redirecting to the Home.html page after clicking submit on form

I am fairly new to Flask and Python and have been just watching Youtube videos of Corey Schafer for flask development and just tailoring it to my specific needs.

I am having troubles redirecting to the home page after I click submit button once I fill out the form. So, when I click submit, it just reloads the page and prints on the top "Submission Unsuccessful". I am sorry if this is a very basic/obvious fix, but I am just lost. All help would be appreciated.

Thanks again!

app.py forms.py requestAQuote.html

{% extends "template.html" %}
{% block content %}
    <div class="content-section">
        <form method="POST" action="">
            {{ form.hidden_tag() }}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Request a Quote today!</legend>
                <div class="form-group">
                    {{ form.gallons_requested.label(class="form-control-label") }}

                    {% if form.gallons_requested.errors %}
                        {{ form.gallons_requested(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.gallons_requested.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.gallons_requested(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.delivery_date.label(class="form-control-label") }}
                    {% if form.delivery_date.errors %}
                        {{ form.delivery_date(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.delivery_date.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.delivery_date(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.request_date.label(class="form-control-label") }}
                    {% if form.request_date.errors %}
                        {{ form.request_date(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.request_date.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.request_date(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.delivery_location.label(class="form-control-label") }}
                    {% if form.delivery_location.errors %}
                        {{ form.delivery_location(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.delivery_location.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.delivery_location(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.delivery_contact_name.label(class="form-control-label") }}
                    {% if form.delivery_contact_name.errors %}
                        {{ form.delivery_contact_name(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.delivery_contact_name.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.delivery_contact_name(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.delivery_contact_phone.label(class="form-control-label") }}
                    {% if form.delivery_contact_phone.errors %}
                        {{ form.delivery_contact_phone(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.delivery_contact_phone.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.delivery_contact_phone(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.delivery_contact_email.label(class="form-control-label") }}
                    {% if form.delivery_contact_email.errors %}
                        {{ form.delivery_contact_email(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.delivery_contact_email.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.delivery_contact_email(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.suggested_price.label(class="form-control-label") }}
                    {% if form.suggested_price.errors %}
                        {{ form.suggested_price(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.suggested_price.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.suggested_price(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.total_amount_due.label(class="form-control-label") }}
                    {% if form.total_amount_due.errors %}
                        {{ form.total_amount_due(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.total_amount_due.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.total_amount_due(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
            </fieldset>
            <div class="form-group">
                {{ form.submit(class="btn btn-outline-info") }}
            </div>
        </form>
    </div>
{% endblock content %}

Upvotes: 1

Views: 86

Answers (1)

Ankur
Ankur

Reputation: 310

In your forms.py, client ID has been created as required field. But you have not mentioned it in your html. Hence form is not getting client id. So everytime you submit the form it does not validate and goes to 'else' condition where it just have to display the unsuccessful message. You will be redirected to home page only when form gets validated.

If client id is a primary field in database, it will auto generate when you save data in db and you don't have to create a form field for it.

If it is not related to database, then in your html, you must create input to enter client id also. Then form will validate because client id has been set as 'data required' in forms.py, which means for the form to validate , it must have value for client id.

Upvotes: 1

Related Questions