0

enter image description here

The issue is the Delete Buttons are not working well, so if I am clicking on Delete Title 1 is deleting Title 1 if I am clicking on Delete Title 2 is deleting Title 1.

This is the code:

@app.route('/addv/<int:users_id>')
def ca_e_5(users_id):
    if 'CM' in current_user.access:
        return redirect(url_for('cmcp', _external=True))

    sca5 = Kca5.query.filter( Kca5.users_id == users_id ).filter( Kca5.ca_st.in_(["a"])).order_by(desc(Kca5.date_posted)).limit(5)

    return render_template('ca/e/ca_e_5.html', sca5=sca5)

@app.route("/uad1", methods=["POST"])
def del_ad1st():
    try:
        nst = request.form.get("nst")
        ost = request.form.get("ost")
        ctca5 = Kca5.query.filter_by(ca_st=ost).first()
        ctca5.ca_st = nst
        db.session.commit()
    except Exception as e:
        print("Couldn't delete!")
        print(e)

    return redirect(url_for('rdr', _external=True))

This is the html code:

<form method="POST" action="{{ url_for('del_ad1st') }}">
                    <input type="hidden" value="{{ctca5.ca_st}}" name="ost">
                    <input type="hidden" value="b" name="nst">
                    <button type="submit" class="btn btn-danger btn-xs" ><i class="fa fa-trash"style="color:white"></i>&nbsp;&nbsp;Delete&nbsp;&nbsp;{{ ctca5.ca_vcn[:100] | safe }}&nbsp;&nbsp;<i class="fa fa-arrow-up"style="color:white"></i></button>
                  </form>
4
  • Could it be the code in the buttons themselves? Do they need different formactions? Commented Apr 8, 2018 at 22:01
  • I was adding now the html code for Delete Button. The ideea is the Delete Button is generated after template ... how shall I make it to be different for each?!!! I am begginer
    – alex
    Commented Apr 8, 2018 at 22:15
  • Sorry I do not understand your code well. It looks like the del_ad1st function deletes the first title always. So if the buttons both call that function (url) they will perform the same action. Commented Apr 8, 2018 at 22:42
  • yes the del_ad1st is the route called by the Delete Button to delete the "post" now I saw on html page source if I am adding more than two posts at each Delete Button action is " action = uad1" if this is the problem how shall generate for each Delete Button different action=uad2 etc? Is it this posibility? Because right now the Delete Button is generated by the template... is it a posibility to generate the delete button from python and to be with unique form action?
    – alex
    Commented Apr 8, 2018 at 22:47

1 Answer 1

0

It sounds like you could have 'n' titles, and it would be nigh impossible to write 'n' delete functions. Notice in MDN button documentation you can assign name to a button, which is submitted with the form data. If each button has a unique name, you would "know" in del_ad1st which button the user clicked, so you would "know" which title to delete. Since the program already gives each button unique text, it should be straightforward to give each a unique name.

6
  • So I was adding a name how is here : <form method="POST" action="{{ url_for('del_ad1st') }}" name="del"> but still it is persisting the issue. But is not generating a unique name only the one is specified in template and is same for each button.
    – alex
    Commented Apr 9, 2018 at 14:50
  • I was trying to give to name even the value from title id which is unique and still not working.
    – alex
    Commented Apr 9, 2018 at 14:58
  • The title is attached on Button from template as you can see: <button type="submit" class="btn btn-danger btn-xs" ><i class="fa fa-trash"style="color:white"></i>&nbsp;&nbsp;Delete&nbsp;&nbsp;{{ ctca5.ca_vcn[:100] | safe }}&nbsp;&nbsp;<i class="fa fa-arrow-up"style="color:white"></i></button> ca_vcn = Title The ideea is why is executing wrong the command?
    – alex
    Commented Apr 9, 2018 at 15:07
  • Last "hint". Read the answer more carefully. Add a name attribute to the button element. Commented Apr 9, 2018 at 16:45
  • Same result even if I am giving a name on button element.
    – alex
    Commented Apr 9, 2018 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .