1

ISSUE

I am making a CRUD in Flask. I have a user that has positions, and I would like to update a specific position.

The "URL was not found" problem occurs when I click on the anchor link in user_positions.html.

I am using WTF Forms, SQLalchemy, PostgreSQL, Bootstrap, and pgAdmin4.

I hope I have given you enough information.


The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

http://0.0.0.0:5000/user/24/url_for('user_position_update',%20owner_id=current_user.id,%20position_id=position.id)%20%7D%7D


MY CODE

user_positions.html

 <a href="{{ url_for('user_position_update', owner_id=current_user.id, position_id=position.id) }}"><i class="mdi mdi-pencil"></i> Update</a>

routes.py

@app.route("/user/<string:owner_id>/positions/<string:position_id>/update", methods=['GET', 'POST'])
def user_position_update(owner_id, position_id):
    user = User.query.filter_by(id=owner_id).first()
    position = Position.query.filter_by(id=position_id).first()
    form = UpdatePositionForm()
    if current_user.is_authenticated:
            if form.validate_on_submit():
                strSQl= "update position set position='"+position+"' where owner_id="+str(user.id)
                db.session.execute(strSQl)
                db.session.commit()
                flash('Your position has been updated!', 'success')
                return redirect(url_for('user_positions'))
            elif request.method == 'GET':
                form.position.data = position
    return render_template('user_position_update.html', form=form, user=user, position=position)

forms.py

class UpdatePositionForm(FlaskForm):
    position = StringField('Position',
                    validators=[DataRequired()])
    date = DateTimeField('Date',
                    validators=[DataRequired()])
    submit = SubmitField('Update')

user_position_update.html

{% extends "layout.html" %}
{% block content %}
    <table class="table">
      <tr>
        <th>Position</th>
        <th>Date</th>
      </tr>
        <tr>
            <form method="POST" enctype="multipart/form-data">
                {{ form.hidden_tag() }}
            <td>
                <div class="form-group">
                    {{ form.positions.label(class="form-control-label") }}
                    <select class="form-control form-control-sm" name="position">
                        <option>{{ current_user.positions[-1].position }}</option>
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>
                        <option>13</option>
                        <option>14</option>
                        <option>15</option>
                      </select>
                </div>
            </td>
            <td>
                <div class="form-group">
                    {{ form.position.label(class="form-control-label") }}
                    <input type="date" class="form-control" value="{{ position.created_date }}" name="date">
                </div>
            </td>
            <td>
                <div class="form-group">
                    {{ form.submit(class="btn btn-outline-info") }}
                </div>
            </td>
        </tr>
    </table>
{% endblock content %}

PostgreSQL tables

user table

enter image description here

position table

enter image description here

Walter Clayton
  • 141
  • 2
  • 7
  • the url_for function is not rendered see , the url is same as you have in href. remove the url and try printing other variable using jinja. look if jinja worked. – charchit Jun 23 '21 at 09:51
  • I still do not understand. Especially "the url is same as you have in href". Which url are you talking about? Could you tell me what I am supposed to replace. Give me an example. Thanks – Walter Clayton Jun 23 '21 at 12:18
  • See according to your href your rendered url should be something like `/user/owner_id/positions/position_id/update` but it is this `url_for('user_position_update', owner_id=current_user.id, position_id=position.id) }` that means your jinja is not working properly. – charchit Jun 23 '21 at 14:27
  • If you give me the code I can test it on my machine. – charchit Jun 23 '21 at 14:28

0 Answers0