Python Forum
view function parameter passed to function but not populateing form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
view function parameter passed to function but not populateing form
#1
Hi, I am trying to display the rows of a database model on a web page as forms that are populated with data that I can then edit. There will be several hundred rows, I can display the rows using a sub template in a for loop, and I can display a form for each row, but it is not populated. I have learnt how to populate a form using "form.populate_obj(obj)", Here is my code.
@bp.route('/stock', methods=['GET', 'POST'])
@bp.route('/stock/<int:id>', methods=['GET', 'POST'])
@login_required
def stock(id=None):
    if id is not None:
        obj = Stock.query.get_or_404(id)
        form = AddStockForm(request.form, obj=obj)
        if form.validate_on_submit():
            form.populate_obj(obj)
            db.session.commit()
        return redirect(url_for('stock.stock'))
    else:
        form = AddStockForm()
        page = request.args.get('page', 1, type=int)
        stock = Stock.query.order_by(Stock.id.desc()).paginate(
             page, current_app.config['ITEMS_PER_PAGE'], False)
        next_url = url_for('stock.stock', page=stock.next_num) \
            if stock.has_next else None
        prev_url = url_for('stock.stock', page=stock.prev_num) \
            if stock.has_prev else None
        return render_template('stock/stock.html',form=form, title=Stock, stock=stock.items,
             next_url=next_url, prev_url=prev_url)
here is the html code stock.html
{% extends "base.html" %}

{% block content %}
<div class="main">
    <h2>Stock</h2>
    <div class="books"><a href="{{ url_for('stock.upload_stock') }}">Upload Stock csv</a></div>
    <div class="books"><a href="{{ url_for('stock.add_stock') }}">Add a new Item</a></div>
    {% for s in stock %}
        {% include {{ url_for('stock._stock', id=['s.id']) }} %}
    {% endfor %}
    {% if prev_url %}
    <a href="{{ prev_url }}">Newer Items</a>
    {% endif %}
    {% if next_url %}
    <a href="{{ next_url }}">Older Items</a>
    {% endif %}
</div>
{% endblock %}
and here is the sub template _stock.html
    <div class="stock">
        <div class="c0"><img src="{{ s.image_url }}" alt="{{ s.image_filename }}"></div>

            <form action="{{ url_for('stock.stock', id=s.id) }}" method="post", enctype="multipart/form-data">
                {{ form.hidden_tag() }}
                <div>SKU<BR> {{ s.id }} </div>
                <div>Date of Purchase<br>{{ form.date(class="input") }}</div>
                <div>Description<br>{{ form.description(class="input") }}</div>
                <div>Market<br>{{ form.event(class="input") }}</div>
                <div>Purchase Price<br>{{ form.achat(class="input") }}</div>
                <div>Sale Price<br>{{ form.vente(class="input") }}</div>
                <div>Sold<br>{{ form.sold(class="input") }}</div>
                <div>{{ form.submit(class="submit") }}</div>
            </form>

    </div>
this the out put
Output:
<form action="/stock/stock/4/" method="post" ,="" enctype="multipart/form-data"> <input id="csrf_token" name="csrf_token" type="hidden" value="ImJhNzkwZTM3YTA4MDBlY2RmOTc5ZWQ2ZjEyOGU3YmU4OTFhYWI4ODki.XdE_qw.19VN-HSMXvVxavHqitix_EHgVNs"> <div>SKU<br> 4 </div> <div>Date of Purchase<br><input class="input" id="date" name="date" required="" type="date" value=""></div> <div>Description<br><input class="input" id="description" name="description" required="" type="text" value=""></div> <div>Market<br><input class="input" id="event" name="event" required="" type="text" value=""></div> <div>Purchase Price<br><input class="input" id="achat" name="achat" required="" type="text" value=""></div> <div>Sale Price<br><input class="input" id="vente" name="vente" type="text" value=""></div> <div>Sold<br><input class="input" id="sold" name="sold" type="checkbox" value="y"></div> <div><input class="submit" id="submit" name="submit" type="submit" value="Submit"></div> </form>
so I dont understand how the output shows the desired paremeter for the function, but the function is not working, I have attached screen shot which shows the output. Sorry there is so much code.
My questions are:
1/ is the sub template the right way
2/ what in my function needs to change to make it work
If I remove the parameter (id=None), When iI start the app I get an error
Error:
werkzeug.routing.BuildError: Could not build url for endpoint 'stock.stock'. Did you forget to specify values ['id']?
Any help or Advice will be greatly needed.
Paul

Attached Files

Thumbnail(s)
   
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020