-1

I have designed a form to update a task. The form will get the details of the task into the update form and while submitting that form the form.validate_on_submit does not work and redirects again to the same form with the previous values. Here are my routes and template code. I have used {{ form.csrf_token }}. But similar logic works with the newTask route.

Update Route

@main.route('/update/<int:id>',methods=['POST','GET'])
@login_required
def update(id):
    if form.validate_on_submit():
        t.task_name = form.task_name.data
        t.assignee = form.assignee.data
        t.deal_size = form.deal_size.data
        t.lead_status = form.lead_status.data
        t.stage = form.stage.data
        t.priority = form.priority.data
        t.submission_date = form.date.data
        db.session.add(t)
        db.session.commit()
        return redirect('/')
    form = NewTaskForm()
    t = Tasks.query.get_or_404(id)
    return render_template("update.html",form = form , t = t)

update.html

<form method="POST" action="/update/{{ t.id }}">
            {{ form.csrf_token }}
            <div class="form-group">
                {{ form.task_name.label }}{{ form.task_name(class='form-control',value = t.task_name ) }}
                {% if form.task_name.errors %}
                    {% for error in form.task_name.errors %}
                        <div class="alert alert-danger" role="alert">
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
            <div class="form-group">
                {{ form.assignee.label }}{{ form.assignee(class='form-control',value = t.assignee) }}
                {% if form.assignee.errors %}
                    {% for error in form.assignee.errors %}
                        <div class="alert alert-danger" role="alert">
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
            <div class="form-group">
                {{ form.deal_size.label }}{{ form.deal_size(class='form-control',value = t.deal_size) }}
                {% if form.deal_size.errors %}
                    {% for error in form.deal_size.errors %}
                        <div class="alert alert-danger" role="alert">
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
            <div class="form-group">
                {{ form.lead_status.label }}{{ form.lead_status(class='form-control',value = t.lead_status) }}
                {% if form.lead_status.errors %}
                    {% for error in form.lead_status.errors %}
                        <div class="alert alert-danger" role="alert">
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
            <div class="form-check padding-none">
                {{ form.stage.label(class='form-check-label') }}{{ form.stage(class='form-check-input customRadio',value = t.stage.data) }}
                {% if form.stage.errors %}
                    {% for error in form.stage.errors %}
                        <div class="alert alert-danger" role="alert">
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
            <div class="form-group">
                {{ form.date.label }}{{ form.date(class='form-control',value = t.submission_date.date()) }}
                {% if form.date.errors %}
                    {% for error in form.date.errors %}
                        <div class="alert alert-danger" role="alert">
                            {{ error }}
                        </div>
                    {% endfor %}
                {% endif %}
            </div>
            {{ form.submit(class="btn btn-outline-primary btn-block") }}
        </form>

newTask route

@main.route('/new-task',methods=['POST','GET'])
@login_required
def newTask():
    form = NewTaskForm()
    if form.validate_on_submit():
        t = Tasks()
        t.task_name = form.task_name.data
        t.assignee = form.assignee.data
        t.deal_size = form.deal_size.data
        t.lead_status = form.lead_status.data
        t.stage = form.stage.data
        t.priority = form.priority.data
        t.submission_date = form.date.data
        db.session.add(t)
        db.session.commit()
        return redirect('/')
    return render_template("newtask.html",form = form)
  • The very first line of the view function references some `form` which was not defined inside the function yet. – VPfB May 17 '20 at 07:19
  • I have defined the form inside a forms.py file and it was imported correctly and it doesn't show any errors. – Allen P Biju May 17 '20 at 07:44
  • But you also define local `form` variable. Note that if the validation with the global `form` fails due to some errors, a new local `form` is displayed. – VPfB May 17 '20 at 10:20
  • But the same thing works for my newTask Route. I have updated the code in my question – Allen P Biju May 17 '20 at 13:06
  • To quickly debug a form not validating, check the validation errors. See: https://stackoverflow.com/questions/24578039/how-to-view-wtforms-validation-errors . My previous comments were written only to make sure the correct `form.errors` is checked. – VPfB May 17 '20 at 13:17

1 Answers1

0

If you want to redirect the user to the new form use:

@main.route('/update/<int:id>',methods=['POST','GET'])
@login_required
def update(id):
    if form.validate_on_submit():
        # do something and then redirect
        return redirect(url_for('confirm'))
    return render_template("update.html",form = form , t = t)

Probably, you need to create a new function for the 'confirmation site' if you haven't done already. You can look up the flask documentation if you want to have a better understanding of url_for :

It accepts the name of the function as its first argument and any number of keyword arguments, each corresponding to a variable part of the URL rule. Unknown variable parts are appended to the URL as query parameters.

Here you can find a good explanation as well: create-dynamic-urls-in-flask

FitAdam
  • 49
  • 6